Skip to content

Instantly share code, notes, and snippets.

@vyo
Created June 8, 2017 13:13
Show Gist options
  • Save vyo/f040791400731679ccbe6687b2037ff6 to your computer and use it in GitHub Desktop.
Save vyo/f040791400731679ccbe6687b2037ff6 to your computer and use it in GitHub Desktop.
public class Util {
/**
* Creates a custom ResourceConfig for use in Jersey Tests.
*
* Registers all resource/class definitions to be used in this test configuration.
*
* Optionally binds specifically configured instances to those definitions, e.g. mocks.
*/
public static ResourceConfig testResourceConfig(Class[] definitions, Object... instances) {
ResourceConfig resourceConfig = new ResourceConfig(definitions);
resourceConfig.register(new AbstractBinder() {
@SuppressWarnings("unchecked") // we're just getting a class here; it's only generics messing
// up inference
@Override
protected void configure() {
for (Object instance : instances) {
bind(instance).to((Class<? super Object>) instance.getClass());
}
}
});
return resourceConfig;
}
/**
* Creates a custom ResourceConfig for use in Jersey Tests.
*
* Registers all resource/class definitions to be used in this test configuration.
*
* Optionally binds specifically configured instances to those definitions, e.g. mocks.
*/
public static ResourceConfig testResourceConfig(Class definition, Object... instances) {
return testResourceConfig(asArray(definition), instances);
}
/**
* Convenience method to turn arbitrary numbers of args into an array.
*
* @param objects the objects to put into a nice tidy, array
* @param <T> the type of the objects passed in
*/
public static <T> T[] asArray(T... objects) {
return objects;
}
/**
* Tries to find an explicitly annotated TestConstructor.
* If none can be found returns the default constructor
*/
public static <T> Constructor<T> findTestConstructor(Class testClass)
throws NoSuchMethodException {
Constructor<T> constructor = (Constructor<T>) Stream
.of(testClass.getDeclaredConstructors())
.filter(c -> c.getDeclaredAnnotation(TestConstructor.class) != null).findFirst()
.orElse(testClass.getDeclaredConstructor());
return constructor;
}
/**
* create an array of mocked objects, one for each constructor parameter
*
* @param constructor the constructor whose parameters shall be mocked
*
* @return an array of mocked objects
*/
public static Object[] createMockConstructorParameters(Constructor constructor) {
Class<?>[] parameterTypes = constructor.getParameterTypes();
return Stream.of(parameterTypes).map(Mockito::mock).toArray();
}
/**
* creates a convenience map for retrieving the mock object for a given constructor param type
*
* entries consist of pairs of
*
* key: simple classe name (string)
* value: mocked class instance (object)
*
* @param arguments the actual mocked objects
*
* @return an
*/
public static Map<String, Object> createArgumentMap(Object[] arguments) {
Map<String, Object> args = new HashMap<>();
Stream.of(arguments).map(a -> new SimpleEntry<>(
a.getClass().getSimpleName().substring(0, a.getClass().getSimpleName().indexOf('$')), a))
.forEach(e -> args.put(e.getKey(), e.getValue()));
return args;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment