Created
August 2, 2016 06:20
-
-
Save ansig/0947f46936ec08eb615fa87a26c5302a to your computer and use it in GitHub Desktop.
Demonstrates how a simple test runner can work using annotations and reflection.
This file contains hidden or 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
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
import java.lang.reflect.Method; | |
public class TestRunner { | |
public static void main(String[] args) { | |
System.out.println("Testing..."); | |
Class<TestClass> testClass = TestClass.class; | |
System.out.println("Test class: " + testClass.getName()); | |
if (testClass.isAnnotationPresent(TestInfo.class)) { | |
TestInfo info = testClass.getAnnotation(TestInfo.class); | |
System.out.println("Created by: " + info.createdBy()); | |
} | |
for (Method method : testClass.getDeclaredMethods()) { | |
if (method.isAnnotationPresent(Test.class)) { | |
System.out.println("Test: " + method.getName()); | |
Test test = method.getAnnotation(Test.class); | |
if (test.enabled()) { | |
try { | |
method.invoke(testClass.newInstance()); | |
System.out.println("\tSuccess"); | |
} catch (Throwable ex) { | |
Throwable cause = ex.getCause(); | |
if (cause instanceof TestFailureException) { | |
System.out.println("\tFail: " + cause.getMessage()); | |
} else { | |
System.out.println("\tError: " + cause.getMessage()); | |
} | |
} | |
} else { | |
System.out.println("\tSkip"); | |
} | |
} | |
} | |
} | |
} | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target(ElementType.TYPE) | |
@interface TestInfo { | |
String createdBy() default "Anonymous"; | |
} | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target(ElementType.METHOD) | |
@interface Test { | |
boolean enabled() default true; | |
} | |
@TestInfo ( | |
createdBy = "Anders Sigfridsson" | |
) | |
class TestClass { | |
@Test | |
void test1() { | |
Assert.assertTrue(true); | |
} | |
@Test | |
void test2() { | |
Assert.assertTrue(true); | |
throw new IllegalArgumentException("Something always goes wrong here!"); | |
} | |
@Test | |
void test3() { | |
Assert.assertTrue(false); | |
} | |
@Test(enabled = false) | |
void test4() { | |
System.out.println("You should not be seeing this line..."); | |
} | |
@Test | |
void test5() { | |
Assert.assertTrue(true); | |
} | |
} | |
class Assert { | |
static void assertTrue(boolean value) throws TestFailureException { | |
if (value != true) { | |
throw new TestFailureException("Expected value to be true"); | |
} | |
} | |
} | |
class TestFailureException extends RuntimeException { | |
TestFailureException(String message) { | |
super(message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment