Created
October 30, 2015 05:00
-
-
Save patrykpoborca/0035e481e70a1133ad3e to your computer and use it in GitHub Desktop.
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
package io.ppoborca.anothertestingapplication; | |
import org.junit.Test; | |
import org.junit.internal.runners.ClassRoadie; | |
import org.junit.internal.runners.MethodRoadie; | |
import org.junit.internal.runners.TestClass; | |
import org.junit.internal.runners.TestMethod; | |
import org.junit.runner.Description; | |
import org.junit.runner.Runner; | |
import org.junit.runner.notification.Failure; | |
import org.junit.runner.notification.RunNotifier; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.util.List; | |
public class MyRunner extends Runner { | |
private final TestClass testClass; | |
private List<Method> methods; | |
public MyRunner(Class<?> klass) { | |
testClass = new TestClass(klass); | |
} | |
@Override | |
public Description getDescription() { | |
Description description = Description.createSuiteDescription(getTestClass().getJavaClass().getName(), getTestClass().getJavaClass().getAnnotations()); | |
methods = testClass.getAnnotatedMethods(Test.class); | |
for(Method method : methods){ | |
description.addChild(methodDescription(method)); | |
} | |
return description; | |
} | |
public TestClass getTestClass() { | |
return testClass; | |
} | |
protected Description methodDescription(Method method) { | |
return Description.createTestDescription(getTestClass().getJavaClass(), method.getName(), method.getAnnotations()); | |
} | |
@Override | |
public void run(final RunNotifier notifier) { | |
new ClassRoadie(notifier, testClass, getDescription(), new Runnable() { | |
public void run() { | |
runMethods(notifier); | |
} | |
}).runProtected(); | |
} | |
protected void runMethods(final RunNotifier notifier) { | |
for (Method method : methods) { | |
invokeTestMethod(method, notifier); | |
} | |
} | |
protected void invokeTestMethod(Method method, RunNotifier notifier) { | |
Description description = methodDescription(method); | |
Object test; | |
try { | |
test = createTest(); | |
} catch (InvocationTargetException e) { | |
testAborted(notifier, description, e.getCause()); | |
return; | |
} catch (Exception e) { | |
testAborted(notifier, description, e); | |
return; | |
} | |
TestMethod testMethod = wrapMethod(method); | |
new MethodRoadie(test, testMethod, notifier, description).run(); | |
} | |
private void testAborted(RunNotifier notifier, Description description, | |
Throwable e) { | |
notifier.fireTestStarted(description); | |
notifier.fireTestFailure(new Failure(description, e)); | |
notifier.fireTestFinished(description); | |
} | |
protected TestMethod wrapMethod(Method method) { | |
return new TestMethod(method, testClass); | |
} | |
protected Object createTest() throws Exception { | |
return getTestClass().getConstructor().newInstance(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment