Skip to content
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions framework/src/play/test/PlayJUnitExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.InvocationInterceptor;
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;
import play.Invoker;
import play.Invoker.DirectInvocation;
import play.Play;
import java.lang.reflect.Method;

public class PlayJUnitExtension implements BeforeAllCallback, BeforeEachCallback, TestExecutionExceptionHandler {
public class PlayJUnitExtension implements BeforeAllCallback, BeforeEachCallback, InvocationInterceptor, TestExecutionExceptionHandler {

public static final String invocationType = "JUnitTest";

public static boolean useCustomRunner = false;

private static final ThreadLocal<Invoker.InvocationContext> invocationContext = ThreadLocal.withInitial(() -> null);
Comment thread
xael-fry marked this conversation as resolved.
Outdated

@Override
public void beforeAll(ExtensionContext context) throws Exception {
Expand All @@ -34,24 +38,46 @@ public void beforeAll(ExtensionContext context) throws Exception {
TestEngine.initTest(testClass);
}


@Override
public void beforeEach(ExtensionContext context) throws Exception {
if (useCustomRunner) {
if (!Play.started) {
Play.forceProd = true;
Play.init(new File("."), getPlayId());
}
// Store invocation context in ThreadLocal for use in interceptTestMethod
invocationContext.set(new Invoker.InvocationContext(invocationType));
Comment thread
xael-fry marked this conversation as resolved.
}
}

@Override
public void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
if (useCustomRunner) {
// Run the test method within the Play thread context
Invoker.InvocationContext ctx = new Invoker.InvocationContext(invocationType);
Throwable[] testException = new Throwable[1];

Invoker.invokeInThread(new DirectInvocation() {
@Override
public void execute() throws Exception {
// Nothing here; test method will run
try {
invocation.proceed();
} catch (Throwable e) {
testException[0] = e;
}
}

@Override
public Invoker.InvocationContext getInvocationContext() {
return new Invoker.InvocationContext(invocationType);
return ctx;
}
});

if (testException[0] != null) {
throw testException[0];
}
} else {
invocation.proceed();
}
}

Expand Down
Loading