The aim is to simplify unit test display names for method names combining CamelCase, underscore and numbers.
Instead of using the method annotation org.junit.jupiter.api.DisplayName
, we can just use the class annotation org.junit.jupiter.api.DisplayNameGeneration
and only use the method annotation if needed.
Applying @DisplayNameGeneration(ReplaceCamelCaseAndUnderscoreAndNumber.class)
to the test class will provide human-readable descriptions for test methods not annotated with @DisplayName
.
3 key rules:
-
Each uppercase letter is turned into its lowercase value prepended by a space.
-
Each underscore is turned into a space. Words bounded by underscores or just starting with underscore are not transformed. Usually, these words represent classes or variables.
-
Each number is prepended by a space.
@DisplayNameGeneration(ReplaceCamelCaseAndUnderscoreAndNumber.class)
Usage Example:
@DisplayNameGeneration(ReplaceCamelCaseAndUnderscoreAndNumber.class)
class ReplaceCamelCaseAndUnderscoreAndNumberStyleTest {
@Test
void shouldReturnErrorWhen_maxResults_IsNegative() {
}
@ParameterizedTest
@ValueSource(strings = { "", " " })
void shouldCreateLimitWithRange(String input) {
}
@ParameterizedTest
@ValueSource(ints = { 5, 23 })
void shouldReturn5Errors(int input) {
}
@Test
void shouldReturn5errors() {
}
@Test
void shouldReturn23Errors() {
}
@Test
void shouldReturnTheValueOf_maxResults() {
}
@ParameterizedTest
@ValueSource(strings = { "", " " })
void shouldReturnTheNumberOfErrorsAs_numberOfErrors_InferiorOrEqualTo5(String input) {
}
@Test
void shouldReturnTheNumberOfErrorsAs_numberOfErrors_InferiorOrEqualTo15() {
}
@DisplayName("@DisplayName prevails")
@Test
void testDisplayNamePrevails() {
}
}
Method 1
Generated display name: Should return error when maxResults is negative
@Test
void shouldReturnErrorWhen_maxResults_IsNegative() {
}
Method 2
Generated display name: Should create limit with range (String)
@ParameterizedTest
@ValueSource(strings = { "", " " })
void shouldCreateLimitWithRange(String input) {
}
Method 3
Generated display name: Should return 5 errors (int)
@ParameterizedTest
@ValueSource(ints = { 5, 23 })
void shouldReturn5Errors(int input) {
}
Method 4
Generated display name: Should return 5 errors
@Test
void shouldReturn5errors() {
}
Method 5
Generated display name: Should return 23 errors
@Test
void shouldReturn23Errors() {
}
Method 6
Generated display name: Should return the value of maxResults
@Test
void shouldReturnTheValueOf_maxResults() {
}
Method 7
Generated display name: Should return the number of errors as numberOfErrors inferior or equal to 5 (String)
@ParameterizedTest
@ValueSource(strings = { "", " " })
void shouldReturnTheNumberOfErrorsAs_numberOfErrors_InferiorOrEqualTo5(String input) {
}
Method 8
Generated display name: Should return the number of errors as numberOfErrors inferior or equal to 15
@Test
void shouldReturnTheNumberOfErrorsAs_numberOfErrors_InferiorOrEqualTo15() {
}
Method 9
Generated display name: @DisplayName prevails
@DisplayName("@DisplayName prevails")
@Test
void testDisplayNamePrevails() {
}