Skip to content

Instantly share code, notes, and snippets.

@joeheyming
Created February 20, 2014 20:12
Show Gist options
  • Save joeheyming/9122224 to your computer and use it in GitHub Desktop.
Save joeheyming/9122224 to your computer and use it in GitHub Desktop.
final getTestObject code
public class TestUtils {
public static <T> T getTestObject(Class<T> type, Object... args) {
Map<String,Object> namedArgs = Utils.getNamedArgs(args);
Constructor <T> constructor = type.getConstructor();
T testobj = constructor.newInstance();
for (Entry<String,Object> entry : namedArgs.getEntrySet()) {
Field field = type.getDeclaredField(entry.getKey());
if (field != null) {
if (Modifier.isPrivate(field.getModifiers()) {
field.setAccessible(true); // this is only for test code
}
field.set(testobj, entry.getValue());
}
}
return testobj;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment