JUnit Display Test Names

Introduction

In this chapter, we will explore how to display custom test names in JUnit. Displaying meaningful test names can make test results more readable and understandable, especially when running a large suite of tests. JUnit provides annotations to customize the names of test methods and parameterized test cases.

Customizing Test Names

JUnit allows you to customize the names of test methods and parameterized test cases using the @DisplayName and @DisplayNameGeneration annotations.

@DisplayName

The @DisplayName annotation is used to define a custom name for a test method or a test class. This annotation helps in providing descriptive names that can improve the readability of test results.

@DisplayNameGeneration

The @DisplayNameGeneration annotation is used at the class level to specify a naming strategy for test methods. This can be useful for applying a consistent naming convention across all test methods in a class.

Example: Customizing Test Names

Let’s create examples to demonstrate how to use the @DisplayName and @DisplayNameGeneration annotations.

Step 1: Create a Class Under Test

Calculator Class

The Calculator class will have basic arithmetic operations.

public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }

    public int subtract(int a, int b) {
        return a - b;
    }

    public int multiply(int a, int b) {
        return a * b;
    }

    public int divide(int a, int b) {
        if (b == 0) {
            throw new IllegalArgumentException("Division by zero");
        }
        return a / b;
    }
}

Step 2: Create a Test Class with Custom Test Names

CalculatorTest

The CalculatorTest class will contain tests for the Calculator class with custom display names.

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

@DisplayName("Calculator Operations Test")
public class CalculatorTest {

    private Calculator calculator = new Calculator();

    @Test
    @DisplayName("Test Addition of Two Numbers")
    void testAdd() {
        assertEquals(5, calculator.add(2, 3), "2 + 3 should equal 5");
    }

    @Test
    @DisplayName("Test Subtraction of Two Numbers")
    void testSubtract() {
        assertEquals(2, calculator.subtract(5, 3), "5 - 3 should equal 2");
    }

    @Test
    @DisplayName("Test Multiplication of Two Numbers")
    void testMultiply() {
        assertEquals(6, calculator.multiply(2, 3), "2 * 3 should equal 6");
    }

    @Test
    @DisplayName("Test Division of Two Numbers")
    void testDivide() {
        assertEquals(2, calculator.divide(6, 3), "6 / 3 should equal 2");
    }

    @Test
    @DisplayName("Test Division by Zero Throws Exception")
    void testDivideByZero() {
        Exception exception = assertThrows(IllegalArgumentException.class, () -> {
            calculator.divide(1, 0);
        });
        assertEquals("Division by zero", exception.getMessage());
    }
}

Step 3: Using @DisplayNameGeneration for Consistent Naming

You can also use the @DisplayNameGeneration annotation to apply a consistent naming strategy to all test methods in a class. Here, we will use the ReplaceUnderscores strategy as an example.

import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
public class ConsistentNamingCalculatorTest {

    private Calculator calculator = new Calculator();

    @Test
    void test_addition_of_two_numbers() {
        assertEquals(5, calculator.add(2, 3), "2 + 3 should equal 5");
    }

    @Test
    void test_subtraction_of_two_numbers() {
        assertEquals(2, calculator.subtract(5, 3), "5 - 3 should equal 2");
    }

    @Test
    void test_multiplication_of_two_numbers() {
        assertEquals(6, calculator.multiply(2, 3), "2 * 3 should equal 6");
    }

    @Test
    void test_division_of_two_numbers() {
        assertEquals(2, calculator.divide(6, 3), "6 / 3 should equal 2");
    }

    @Test
    void test_division_by_zero_throws_exception() {
        Exception exception = assertThrows(IllegalArgumentException.class, () -> {
            calculator.divide(1, 0);
        });
        assertEquals("Division by zero", exception.getMessage());
    }
}

Important Points

  • @DisplayName: Use this annotation to provide custom, descriptive names for individual test methods and classes. This makes the test results more readable.
  • @DisplayNameGeneration: Use this annotation at the class level to apply a consistent naming strategy to all test methods in the class. This can help enforce a naming convention.

Step 4: Running the Tests

To run the tests, simply run the CalculatorTest and ConsistentNamingCalculatorTest classes as JUnit tests. This will execute all test methods with the custom display names.

Using IntelliJ IDEA

  1. Run Tests: Click the green run icon next to the CalculatorTest or ConsistentNamingCalculatorTest class and select Run.
  2. View Results: The results will be displayed in the Run window with the custom display names.

JUnit Display Test Names

Conclusion

JUnit provides powerful annotations like @DisplayName and @DisplayNameGeneration to customize test names. Using these annotations can improve the readability and understandability of test results, making it easier to identify and diagnose issues in your code. Customizing test names helps create more descriptive and maintainable test suites, especially for large projects.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top