Created
March 24, 2014 18:15
-
-
Save set321go/9745900 to your computer and use it in GitHub Desktop.
Contract Test Example
This file contains 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
@RunWith(Parameterized.class) | |
public class ApiContractTest { | |
private UrlValidator urlValidator = new UrlValidator(new String[]{"http","https"}); | |
private Api toTest; | |
public ApiContractTest(Api impl, String name){ | |
this.toTest = impl; | |
} | |
@Parameterized.Parameters(name = "{1}") | |
public static Collection<Object[]> generateData() throws IllegalAccessException, InstantiationException { | |
Reflections reflections = new Reflections("org.scribe.builder.api"); | |
Set<Class<? extends Api>> allSubTypes = reflections.getSubTypesOf(Api.class); | |
List<Object[]> allConcreteSubTypes = new ArrayList<>(); | |
for (Class<? extends Api> candidate : allSubTypes) { | |
if(!Modifier.isAbstract(candidate.getModifiers())){ | |
allConcreteSubTypes.add(new Object[]{candidate.newInstance(), candidate.getSimpleName()}); | |
} | |
} | |
return allConcreteSubTypes; | |
} | |
@Test | |
public void ifHasGrantTypeReturnsValidGrantType(){ | |
if(toTest.hasGrantType()){ | |
assertTrue("getGrantType Breaches the provider Api contract, " + | |
"If you specify the api uses grant_type you must provide a valid grant type", StringUtils.isNotBlank(toTest.getGrantType())); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
Parameters(name = "{1}")
and theString name
on the constructor are just another artifact of the clunkiness of junit paramterized tests. All this is doing is giving the tests a name instead of numbers.