Junit 5 Simple 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;
import static org.junit.jupiter.api.DisplayNameGenerator.Simple;

@DisplayNameGeneration(Simple.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");
        }
    }
}
plugins {
    id 'java'
}

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

repositories {
    mavenCentral()
}

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

 

Simple Display Name Generator Configuration Parameter

Simple 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.junit.jupiter.api.DisplayNameGenerator$Simple
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