Modern technology gives us many things.

How to Create Nested Tests in JUnit


A well-written test suite will have several tests for a specific feature. By grouping related features, you can express the relationship between several groups of tests. One of the major benefits of grouping unit test classes is that it can reduce the amount of test code you write, as grouped tests share resources.

JUnit 5 allows you to create nested tests using the @Nested annotation. In this article, you will learn what the @Nested annotation is and how to use it.

What Is a Nested Test?

JUnit’s @Nested annotation signals that the class it associates with is an inner class, which is a member of another class. A nested test is a test class that contains the @Nested annotation, as this means that there is one (or more) inner class within a top-level test class. A nested class can appear within a top-level class, or within a class that is also nested.

Creating Java Classes to Test

A nested test class inherits all the features of its parent class. Therefore, the best time to create a nested test is when there is a logical grouping of test cases or when a single test case has different features. A good example of this is when you want to test a class’s ability to create useful objects. Another example is when a single method has two or more purposes.

Here’s an example class that you might use in a retail application, that demonstrates how you can create a nested test.

public class Customer {
protected int customerId;
protected String customerName;
protected String customerCode;

public Customer() {
this.customerId = 0;
this.customerName = "";
this.customerCode ="";

public Customer(int customerId, String customerName, String customerCode) {
this.customerId = customerId;
this.customerName = customerName;
this.customerCode = customerCode;

public Customer(Customer customer) {
this.customerId = customer.customerId;
this.customerName = customer.customerName;
this.customerCode = customer.customerCode;

public int getCustomerId() {
return customerId;

public void setCustomerId(int customerId) {
this.customerId = customerId;

public String getCustomerName() {
return customerName;

public void setCustomerName(String customerName) {
this.customerName = customerName;

public String getCustomerCode() {
return customerCode;

public void setCustomerCode(String customerCode) {
this.customerCode = customerCode;

public double customerType(String customerCode) {
double discount = 0;

if (customerCode.toLowerCase().equals("pre")) {
discount = 0.10;
} else if (customerCode.toLowerCase().equals("gen")) {
discount = 0.02;
} else if (customerCode.toLowerCase().equals("new")) {
discount = 0.05;

return discount;

public double grandTotal(double total) {
double discount = customerType(customerCode);
double discountPercentage = total * discount;
double finalTotal = total - discountPercentage;
return finalTotal;

This Customer class contains all the components of a Java Class, complete with two methods.

Creating a Nested Test With JUnit 5

The Customer class has several constructors, getters, and setters, and two methods. You can create a nested class (within the Customer test class) that creates a new Customer object and tests all its components.

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

@DisplayName("Customer Test Class Showing How to Create Nested Tests.")
class CustomerTest {
protected int customerId = 301;
protected String customerName = "Mike Wilson";
protected String customerCode = "Pre";
protected double total = 600;

@DisplayName("Customer Builder Nested Test Class Within a Top-Level Test Class")
class CustomerBuilderTest {
Customer customer = new Customer(customerId, customerName, customerCode);
double grandTotal = customer.grandTotal(total);

@DisplayName("Testing the Customer's Class Constructors, Getters and Setters, and Methods.")
void customerBuilder() {
assertAll(() -> {
assertEquals(customerId, customer.getCustomerId());
assertEquals(customerName, customer.getCustomerName());
assertEquals(customerCode, customer.getCustomerCode());
assertEquals(0.10, customer.customerType(customerCode));
assertEquals(540, grandTotal);

The CustomerTest class is the top-level test class to a nested CustomerBuilderTest class. CustomerBuilderTest creates a new Customer object and tests its components using assertion tests.

Executing the CustomerTest test class produces the following successful test results:

The names of the test classes and the test method are descriptive and comprehensive, thanks to the @DisplayName Annotation.

Knowing How to Test Software is Crucial

Technology is an important aspect of everyday life for most people. The stakes for creating software that does exactly what it is supposed to do have never been higher.

A self-driving car that miscalculates its proximity to another object can cause a major accident. Therefore, you need to test your application at every stage of its development.

Source link

Leave A Reply

Your email address will not be published.