Last active
October 11, 2015 13:27
-
-
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 :)
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
/** | |
* 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 ([email protected] @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