Junit 5 Custom Display Name Generator

Junit 5 defines all the requirements of the Display Name Generator in the DisplayNameGenerator interface. It allows to implement the interface and create a custom Display Name Generator.

Simple Display Name Generator extends Standard Display Name Generator. It removes the trailing parentheses of methods with no parameters.

package org.wesome.junit5;

public class AppleCalculator {
    public int addApple(int apple1, int apple2) {
        int apple = apple1 + apple2;
        return apple;
    }
}
package org.wesome.junit5;

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

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

@DisplayNameGeneration(CustomDisplayNameGenerator.class)
class Apple_Calculator_Test {
    @Test
    void add_Apple() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Nested
    class Apple_Calculator_Nested_Test {
        @Test
        void add_Apple() {
            AppleCalculator appleCalculator = new AppleCalculator();
            assertEquals(4, appleCalculator.addApple(2, 2), "2 apple + 2 apple is 4 apple");
        }
    }
}
package org.wesome.junit5;

import org.junit.jupiter.api.DisplayNameGenerator;

import java.lang.reflect.Method;

public class CustomDisplayNameGenerator implements DisplayNameGenerator {

    @Override
    public String generateDisplayNameForClass(Class<?> testClass) {
        String name = testClass.getName();
        int lastDot = name.lastIndexOf('_');
        return name.substring(lastDot + 1);
    }

    @Override
    public String generateDisplayNameForNestedClass(Class<?> nestedClass) {
        return nestedClass.getSimpleName();
    }

    @Override
    public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod) {
        String className = generateDisplayNameForClass(testClass);
        String methodName = testMethod.getName();
        return className + "=>" + methodName;
    }
}
plugins {
    id 'java'
}

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

repositories {
    mavenCentral()
}

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

 

Custom Display Name Generator Configuration Parameter

Custom Display Name Generator can be configured as Configuration Parameter in src/test/resources/junit-platform.properties file.

package org.wesome.junit5;

public class AppleCalculator {
    public int addApple(int apple1, int apple2) {
        int apple = apple1 + apple2;
        return apple;
    }
}
package org.wesome.junit5;

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

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

class Apple_Calculator_Test {
    @Test
    void add_Apple() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Nested
    class Apple_Calculator_Nested_Test {
        @Test
        void add_Apple() {
            AppleCalculator appleCalculator = new AppleCalculator();
            assertEquals(4, appleCalculator.addApple(2, 2), "2 apple + 2 apple is 4 apple");
        }
    }
}
junit.jupiter.displayname.generator.default=org.wesome.junit5.CustomDisplayNameGenerator
plugins {
    id 'java'
}

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

repositories {
    mavenCentral()
}

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

follow us on