Skip to content

Commit

Permalink
Merge pull request #21 from Nosto/feature/test-annotation-classloader
Browse files Browse the repository at this point in the history
Modified the Play test runner to be Gradle compatible.
  • Loading branch information
mridang authored Oct 31, 2016
2 parents 6f15a3d + 2939121 commit 8b44d20
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions framework/src/play/test/PlayJUnitRunner.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package play.test;

import java.io.File;
import java.lang.annotation.Annotation;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.junit.rules.MethodRule;
import org.junit.runner.Description;
Expand All @@ -9,6 +11,7 @@
import org.junit.runner.manipulation.Filterable;
import org.junit.runner.manipulation.NoTestsRemainException;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.JUnit4;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
Expand All @@ -19,16 +22,19 @@
import play.Invoker.DirectInvocation;
import play.Play;

public class PlayJUnitRunner extends Runner implements Filterable {
public class PlayJUnitRunner extends BlockJUnit4ClassRunner implements Filterable {

public static final String invocationType = "JUnitTest";

public static boolean useCustomRunner = false;

// *******************
JUnit4 jUnit4;
private final Class tclass;

public PlayJUnitRunner(Class testClass) throws ClassNotFoundException, InitializationError {
public PlayJUnitRunner(Class testClass) throws InitializationError {
super(resolve(testClass));
this.tclass = testClass;
}

private static Class resolve(Class testClass) {
synchronized (Play.class) {
if (!Play.started) {
Play.init(new File("."), PlayJUnitRunner.getPlayId());
Expand All @@ -39,8 +45,7 @@ public PlayJUnitRunner(Class testClass) throws ClassNotFoundException, Initializ
}
useCustomRunner = true;
}
Class<?> classToRun = Play.classloader.loadApplicationClass(testClass.getName());
jUnit4 = new JUnit4(classToRun);
return Play.classloader.loadApplicationClass(testClass.getName());
}
}

Expand All @@ -53,12 +58,17 @@ private static String getPlayId() {
}

@Override
public Description getDescription() {
return jUnit4.getDescription();
protected Annotation[] getRunnerAnnotations() {
return tclass.getAnnotations();
}

@Override
protected Description describeChild(FrameworkMethod method) {
return Description.createTestDescription(tclass, testName(method), method.getAnnotations());
}

private void initTest() {
TestClass testClass = jUnit4.getTestClass();
TestClass testClass = getTestClass();
if(testClass != null){
TestEngine.initTest(testClass.getJavaClass());
}
Expand All @@ -67,15 +77,9 @@ private void initTest() {
@Override
public void run(final RunNotifier notifier) {
initTest();
jUnit4.run(notifier);
super.run(notifier);
}

@Override
public void filter(Filter toFilter) throws NoTestsRemainException {
jUnit4.filter(toFilter);

}

// *********************
public enum StartPlay implements MethodRule {

Expand Down

0 comments on commit 8b44d20

Please sign in to comment.