Junit 5 @DisplayName

Production code has lots of methods, each method has multiple test cases, then it becomes hard to actually distinguish the test case type by just looking at the method name.
JUnit provides @DisplayName annotation to add display names to easily identify the test cases.

package com.example.junit5.sujan;

public class AppleCalculator {
    public int addApple(int appleBag1, int appleBag2) {
        return appleBag1 + appleBag2;
    }
}
package com.example.junit5.sujan;

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

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

@DisplayName("Apple Calculator Test")
class AppleCalculatorTest {

    @Test
    @DisplayName("Apple Calculator Addition Test")
    void addApple() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    @DisplayName("Apple Calculator Addition Test (°□°)")
    void addAppleWithSpecialCharacter() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(4, appleCalculator.addApple(2, 2), "2 apple + 2 apple is 4 apple");
    }

    @Test
    @DisplayName("Apple Calculator Addition Test ?")
    void addAppleWithEmoji() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(6, appleCalculator.addApple(3, 3), "3 apple + 3 apple is 6 apple");
    }
}

plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

follow us on