diff --git a/testing-modules/junit-5-advanced/pom.xml b/testing-modules/junit-5-advanced/pom.xml new file mode 100644 index 0000000000..f65f7e2a2f --- /dev/null +++ b/testing-modules/junit-5-advanced/pom.xml @@ -0,0 +1,32 @@ + + + 4.0.0 + junit-5-advanced + 1.0-SNAPSHOT + junit-5-advanced + Advanced JUnit 5 Topics + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + + + + + org.junit.jupiter + junit-jupiter + ${junit-jupiter.version} + test + + + + + 5.4.2 + 2.21.0 + + + diff --git a/testing-modules/junit-5-advanced/src/test/java/com/baeldung/displayname/DisplayNameGeneratorUnitTest.java b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/displayname/DisplayNameGeneratorUnitTest.java new file mode 100644 index 0000000000..311539f760 --- /dev/null +++ b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/displayname/DisplayNameGeneratorUnitTest.java @@ -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; itesting testng junit-5-basics + junit-5-advanced