BAEL-2911 - JUnit Custom Display Name Generator API
This commit is contained in:
parent
92989e4556
commit
a11e0088ba
|
@ -0,0 +1,32 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>junit-5-advanced</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>junit-5-advanced</name>
|
||||
<description>Advanced JUnit 5 Topics</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<junit-jupiter.version>5.4.2</junit-jupiter.version>
|
||||
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,87 @@
|
|||
package com.baeldung.displayname;
|
||||
|
||||
import org.junit.jupiter.api.DisplayNameGeneration;
|
||||
import org.junit.jupiter.api.DisplayNameGenerator;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@DisplayNameGeneration(DisplayNameGeneratorUnitTest.ReplaceCamelCase.class)
|
||||
class DisplayNameGeneratorUnitTest {
|
||||
|
||||
@Test
|
||||
void camelCaseName() {
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayNameGeneration(DisplayNameGeneratorUnitTest.IndicativeSentences.class)
|
||||
class ANumberIsFizz {
|
||||
@Test
|
||||
void ifItIsDivisibleByThree() {
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "Number {0} is fizz.")
|
||||
@ValueSource(ints = { 3, 12, 18 })
|
||||
void ifItIsOneOfTheFollowingNumbers(int number) {
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayNameGeneration(DisplayNameGeneratorUnitTest.IndicativeSentences.class)
|
||||
class ANumberIsBuzz {
|
||||
@Test
|
||||
void ifItIsDivisibleByFive() {
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "Number {0} is buzz.")
|
||||
@ValueSource(ints = { 5, 10, 20 })
|
||||
void ifItIsOneOfTheFollowingNumbers(int number) {
|
||||
}
|
||||
}
|
||||
|
||||
static class IndicativeSentences extends ReplaceCamelCase {
|
||||
@Override
|
||||
public String generateDisplayNameForNestedClass(Class<?> nestedClass) {
|
||||
return super.generateDisplayNameForNestedClass(nestedClass) + "...";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod) {
|
||||
return replaceCamelCase(testClass.getSimpleName() + " " + testMethod.getName()) + ".";
|
||||
}
|
||||
}
|
||||
|
||||
static class ReplaceCamelCase extends DisplayNameGenerator.Standard {
|
||||
@Override
|
||||
public String generateDisplayNameForClass(Class<?> testClass) {
|
||||
return replaceCamelCase(super.generateDisplayNameForClass(testClass));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateDisplayNameForNestedClass(Class<?> nestedClass) {
|
||||
return replaceCamelCase(super.generateDisplayNameForNestedClass(nestedClass));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod) {
|
||||
return this.replaceCamelCase(testMethod.getName()) + DisplayNameGenerator.parameterTypesAsString(testMethod);
|
||||
}
|
||||
|
||||
String replaceCamelCase(String camelCase) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
result.append(camelCase.charAt(0));
|
||||
for (int i=1; i<camelCase.length(); i++) {
|
||||
if (Character.isUpperCase(camelCase.charAt(i))) {
|
||||
result.append(' ');
|
||||
result.append(Character.toLowerCase(camelCase.charAt(i)));
|
||||
} else {
|
||||
result.append(camelCase.charAt(i));
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -33,5 +33,6 @@
|
|||
<module>testing</module>
|
||||
<module>testng</module>
|
||||
<module>junit-5-basics</module>
|
||||
<module>junit-5-advanced</module>
|
||||
</modules>
|
||||
</project>
|
||||
|
|
Loading…
Reference in New Issue