Skip to content

Instantly share code, notes, and snippets.

@set321go
Created March 24, 2014 18:15
Show Gist options
  • Save set321go/9745900 to your computer and use it in GitHub Desktop.
Save set321go/9745900 to your computer and use it in GitHub Desktop.
Contract Test Example
@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()));
}
}
}
@set321go
Copy link
Author

The Parameters(name = "{1}") and the String 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment