Skip to content

Instantly share code, notes, and snippets.

@michalbcz
Last active October 11, 2015 13:27
Show Gist options
  • Save michalbcz/3865374 to your computer and use it in GitHub Desktop.
Save michalbcz/3865374 to your computer and use it in GitHub Desktop.
for those who have problem with writing long test methods name like me :)
/**
* Using this runner every test report (gui, cli, html) will print names of tests not as test method
* names eg. "shouldBeLikeThatAndThat" but with spaces like "should be like that and that" for better
* readability. <br/><br/>
*
* <b>Usage</b>:<br/>
*
* <pre>
* <b>{@literal @}RunWith(ReadableTestNamesJUnitRunner.class)</b>
* public class DateParserTest {
*
* {@literal @}Test
* public void shouldParseDateMonthYearWithSpacesBetweenValuesAndSingleDigitDay() {
* ...
* }
*
* }
* </pre>
*
* @author Michal Bernhard 2012 (michal@bernhard.cz @michalb_cz)
*
*/
public class ReadableTestNamesJUnitRunner extends BlockJUnit4ClassRunner {
private static final Pattern CAPITALIZED_WORD_MATCHER_PATTERN = Pattern.compile("([A-Z][a-z]*)");
public ReadableTestNamesJUnitRunner(Class<?> klass) throws InitializationError {
super(klass);
}
@Override
protected String testName(FrameworkMethod method) {
return convertLowerCamelCaseToWordsDelimitedBySpace(method.getName());
}
private String convertLowerCamelCaseToWordsDelimitedBySpace(String name) {
String capitalizedName = StringUtils.capitalize(name);
Matcher matcher = CAPITALIZED_WORD_MATCHER_PATTERN.matcher(capitalizedName);
StringBuilder sb = new StringBuilder();
while(matcher.find()) {
sb.append(matcher.group(1));
sb.append(" ");
}
return sb.toString().toLowerCase();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment