implemented Tester.runAllTests() and @Test annotation

This commit is contained in:
Kjistóf 2016-04-25 19:27:29 +02:00
parent 43653de8c9
commit e8bdc59081

View File

@ -3,11 +3,18 @@ package cicaprojekt;
import java.io.Console; import java.io.Console;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Tester { public class Tester {
Player oneill; private Player oneill;
Player jaffa; private Player jaffa;
PlayerBase replicator; private PlayerBase replicator;
Dungeon loadMap(String param) throws IOException{ Dungeon loadMap(String param) throws IOException{
Dungeon dungeon = new Dungeon(); Dungeon dungeon = new Dungeon();
@ -21,5 +28,27 @@ public class Tester {
System.out.println(replicator); System.out.println(replicator);
} }
/* custom Test annotation */
@Target(ElementType.METHOD) // it's for methods
@Retention(RetentionPolicy.RUNTIME) // we want to retain annotations in runtime
private @interface Test {}
/* run all methods annotated with @Test */
public boolean runAllTests() throws InvocationTargetException, IllegalAccessException
{
boolean testspassed = true;
for (Method m : this.getClass().getMethods()) // iterate over all methods of this
{
if (m.isAnnotationPresent(Test.class)) // if its annotated with @Test...
{
Boolean testresult = (Boolean) m.invoke(null); // call it!
if (!testresult)
{
testspassed = false;
}
}
}
return testspassed;
}
} }