The Replace Underscores Display Name Generator extends the Simple Display Name Generator. It replaces all underscores of class and methods with space.
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.ReplaceUnderscores;
@DisplayNameGeneration(ReplaceUnderscores.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()
}
Replace Underscores Display Name Generator Configuration Parameter
Replace Underscores 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$ReplaceUnderscores
plugins {
id 'java'
}
group 'org.wesome'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}
test {
useJUnitPlatform()
}