VOOZH about

URL: https://www.geeksforgeeks.org/advance-java/nested-map-assertions-in-java-with-junit/

⇱ Nested Map Assertions in Java with JUnit - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Nested Map Assertions in Java with JUnit

Last Updated : 23 Jul, 2025

In Java applications, nested data structures are commonplace, especially when dealing with configurations, JSON responses, or complex data representations. One such structure is the nested map. This article focuses on the theory behind nested maps, their structures, and the techniques used for asserting their contents effectively in unit tests using JUnit.

Prerequisites:

  • Basic understanding of Java and the JUnit Framework.
  • Maven for dependency management.
  • JDK and IntelliJ IDEA installed on your system.

Maven Dependency

Add the following Maven dependency to the pom.xml file:

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>

Understanding Nested Maps

What is a Map?

In Java, a Map is an interface that associates unique keys with specific values, forming part of the Java Collections Framework, and is utilized for storing data in key-value pairs.. Here are some characteristics of maps:

  • Dynamic Size: Maps can dynamically grow as new key-value pairs are added.
  • Common Implementations: The most commonly used implementations of the Map interface include:
    • HashMap: Provides constant uptime for basic operations and does not maintain any order.
    • LinkedHashMap: checks which entries in the map are linked, and stores the order of insertion.
    • TreeMap: Uses a sorted map, sorting keys in their natural order or based on a given comparator.

What is a Nested Map?

A nested map is a map where the values themselves are maps. This format allows for a more systematic organization of the data. For example, consider a nested map representing a library system in which each type of book (e.g., fiction, nonfiction) is a collection of books along with their description (e.g. title, author, quantity).

Example of a Nested Map Structure:

Map<String, Map<String, Integer>> library = new HashMap<>();
library.put("Fiction", Map.of("1984", 3, "Brave New World", 5));
library.put("Non-Fiction", Map.of("Sapiens", 2, "Educated", 4));

Real-World Use Case of Employee Management System

In this example, we will create a nested map structure that represents an employee management system. Each department will have multiple employees, and each employee will have a record of projects.

The data structure will look like this:

  • Outer Map: Key = Department, Value = Inner Map
  • Inner Map: Key = Employee Name, Value = Employee Object
  • Employee Object: Contains employee details such as ID, salary, and a List<String> representing their projects.

Step 1: Create a New Maven Project

Create a new Maven project using IntelliJ IDEA with the following options:

  • Name: nested-map-assertion
  • Build system: Maven

Click on the Create button.

👁 Project Metadata

Project Structure

Once project creation done, set the folder structure as shown in the below image:

👁 Project Folder Structure

Step 2: Add the JUnit 5 dependencies to pom.xml

Open the pom.xml file and add the following JUnit 5 dependencies into the Maven project.

Step 3: Define the Employee Model

Define the Employee model class and add the following code into it.

Step 4: Create the EmployeeDataProvider Class

Create the EmployeeDataProvider.java file, which will return the nested map:

Step 5: MainApplication Class

In the main class, add logic to print the employee details as output:

Step 5: Create the Test Class

Create the EmployeeTest.java file and this class contains the unit tests to assert the nested map.

Explanation:

  • Test Setup: The testEmployeeDataStructure() method retrieves the nested map structure from the EmployeeDataProvider.
  • Asserting the Outer Map: The outer map represents the departments, and we first assert that there are exactly the two departments.
  • Asserting the Inner Map: For the each department, we assert that the correct number of the employees is present.
  • Asserting Employee Details: We go one level deeper to the assert each employee’s id, salary, and the list of the projects they are working on it.

Step 6: Run the Application

Once the project is completed, we will run and it will display the below output:

👁 Console Output

Step 7: Running the Test

We can execute the test suite using following maven command.

mvn test

Output:

All assertions should pass if the method behaves as the expected.

👁 Test Output

This example project demonstrated how to assert the nested maps in the JUnit effectively. This example focused on the employee management system with multiple levels of the nesting, including the lists inside the maps. Such structures are common in the real world applications, and this method can ensure and thoroughly validate the data structure using clear and concise assertions.

Comment
Article Tags:
Article Tags:

Explore