Created
February 20, 2014 20:12
-
-
Save joeheyming/9122224 to your computer and use it in GitHub Desktop.
final getTestObject code
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
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