Skip to content

Instantly share code, notes, and snippets.

@vyo
Created June 8, 2017 13:13
Show Gist options
  • Save vyo/76b93765de4a2449d22de809dbae57fd to your computer and use it in GitHub Desktop.
Save vyo/76b93765de4a2449d22de809dbae57fd to your computer and use it in GitHub Desktop.
public abstract class ControllerTest<T> extends JerseyTest {
private Class<T> controllerClass;
private T controller;
private Map<String, Object> args;
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Override
public Application configure() {
try {
init();
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) {
System.err.println(e);
System.err.println(e.getMessage());
e.printStackTrace(System.err);
fail();
}
return testResourceConfig(asArray(EchoUserProvider.class, controllerClass), controller);
}
private void init()
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
controllerClass = (Class<T>)
((ParameterizedType) getClass().getGenericSuperclass())
.getActualTypeArguments()[0];
Constructor<T> testConstructor = findTestConstructor(controllerClass);
Object[] parameters = createMockConstructorParameters(testConstructor);
args = createArgumentMap(parameters);
controller = testConstructor.newInstance(parameters);
}
protected T getController() {
return controller;
}
protected Object getField(Class argClass) {
return args.get(argClass.getSimpleName());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment