1 package junitparams.naming; 2 3 import org.junit.AfterClass; 4 import org.junit.Ignore; 5 import org.junit.Test; 6 import org.junit.runner.Description; 7 import org.junit.runner.Request; 8 import org.junit.runner.RunWith; 9 10 import junitparams.JUnitParamsRunner; 11 import junitparams.Parameters; 12 13 import static org.assertj.core.api.Assertions.*; 14 15 @RunWith(JUnitParamsRunner.class) 16 public class NamingStrategyIsUsedByRunnerTest { 17 18 @AfterClass checkTestCaseNames()19 public static void checkTestCaseNames() { 20 Description rootDescription = getTestClassDescription(); 21 String className = "(" + NamingStrategyIsUsedByRunnerTest.class.getCanonicalName() + ")"; 22 23 Description sampleMethodDescription = getChildDescriptionByName(rootDescription, "sampleMethod"); 24 25 assertThat(sampleMethodDescription.getChildren()).extracting("displayName").containsExactly( 26 "[0] Well formed name of sampleMethod with param1" + className, 27 "[1] Well formed name of sampleMethod with param2" + className); 28 } 29 30 // Android-changed: CTS and AndroidJUnitRunner rely on specific format to test names, changing 31 // them will prevent CTS and AndroidJUnitRunner from working properly; see b/36541809 32 @Ignore 33 @Test 34 @Parameters({"param1", "param2"}) 35 @TestCaseName("[{index}] Well formed name of {method} with {params}") sampleMethod(String parameter)36 public void sampleMethod(String parameter) { 37 } 38 getTestClassDescription()39 private static Description getTestClassDescription() { 40 return Request.aClass(NamingStrategyIsUsedByRunnerTest.class).getRunner().getDescription(); 41 } 42 getChildDescriptionByName(Description parent, String expectedName)43 private static Description getChildDescriptionByName(Description parent, String expectedName) { 44 for (Description childDescription : parent.getChildren()) { 45 if (expectedName.equals(childDescription.getDisplayName())) { 46 return childDescription; 47 } 48 } 49 50 return null; 51 } 52 } 53