Last active
February 16, 2023 14:56
-
-
Save dehidehidehi/aea63c73a7206fa7eb6a134099df17b9 to your computer and use it in GitHub Desktop.
JUnit5 Auto DisplayName Generator: Capitalized Camel Case tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# put this in test/resources dir | |
junit.jupiter.displayname.generator.default=\ | |
PATH_TO_CLASS.ReplaceCapitalizedCamelCase |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.jupiter.api.DisplayNameGenerator; | |
import java.lang.reflect.Method; | |
/** | |
* Classe permettant à jUnit d'automatiquement transformer les noms de classes en phrases lors de l'exécution des tests.<br> | |
* Permet une meilleure lisibilité. | |
*/ | |
public class ReplaceCapitalizedCamelCase extends DisplayNameGenerator.Standard { | |
public ReplaceCapitalizedCamelCase() { | |
} | |
@Override | |
public String generateDisplayNameForClass(final Class<?> testClass) { | |
return this.replaceCapitals(super.generateDisplayNameForClass(testClass)); | |
} | |
@Override | |
public String generateDisplayNameForNestedClass(final Class<?> nestedClass) { | |
return this.replaceCapitals(super.generateDisplayNameForNestedClass(nestedClass)); | |
} | |
@Override | |
public String generateDisplayNameForMethod(final Class<?> testClass, final Method testMethod) { | |
return this.replaceCapitals(testMethod.getName()); | |
} | |
private String replaceCapitals(String name) { | |
name = name.replaceAll("([A-Z])", " $1"); // adding spaces | |
name = name.replaceAll("([0-9]+)", " $1"); // adding spaces | |
name = name.substring(0, 1).toUpperCase() + name.substring(1); // forcing first char capitalized | |
return name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment