Skip to content

Instantly share code, notes, and snippets.

@freekman
Created January 7, 2015 14:49
Show Gist options
  • Select an option

  • Save freekman/bbe19ee104ed07b5fa8f to your computer and use it in GitHub Desktop.

Select an option

Save freekman/bbe19ee104ed07b5fa8f to your computer and use it in GitHub Desktop.
Sample Test
public static class MyTest {
public void testAddTwoNumbers() {
System.out.println("Adding two numbers");
}
public Date testJanuary(int year, int day) {
System.out.println("Creating new date for january....");
return new Date();
}
}
public static interface TestObject {
void another();
}
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
runTests(MyTest.class);
}
private static void runTests(Class<?> clazz) throws IllegalAccessException, InstantiationException {
Method[] methods = clazz.getDeclaredMethods();
Object o = clazz.newInstance();
Object[] emptyArgs = {};
for (Method each : methods) {
String methodName = each.getName();
if (methodName.startsWith("test")) {
// move to the next method in case when
// method parameters are more then zero
if (each.getParameterTypes().length != 0) {
continue;
}
try {
each.invoke(o, emptyArgs);
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment