Skip to content

Commit 83c944e

Browse files
committed
Update PlayJUnitExtension.java with new implementation
1 parent 17eaace commit 83c944e

1 file changed

Lines changed: 30 additions & 4 deletions

File tree

framework/src/play/test/PlayJUnitExtension.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55
import org.junit.jupiter.api.extension.BeforeAllCallback;
66
import org.junit.jupiter.api.extension.BeforeEachCallback;
77
import org.junit.jupiter.api.extension.ExtensionContext;
8+
import org.junit.jupiter.api.extension.InvocationInterceptor;
89
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
10+
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;
911
import play.Invoker;
1012
import play.Invoker.DirectInvocation;
1113
import play.Play;
14+
import java.lang.reflect.Method;
1215

13-
public class PlayJUnitExtension implements BeforeAllCallback, BeforeEachCallback, TestExecutionExceptionHandler {
16+
public class PlayJUnitExtension implements BeforeAllCallback, BeforeEachCallback, InvocationInterceptor, TestExecutionExceptionHandler {
1417

1518
public static final String invocationType = "JUnitTest";
1619

1720
public static boolean useCustomRunner = false;
1821

22+
private static final ThreadLocal<Invoker.InvocationContext> invocationContext = ThreadLocal.withInitial(() -> null);
1923

2024
@Override
2125
public void beforeAll(ExtensionContext context) throws Exception {
@@ -34,24 +38,46 @@ public void beforeAll(ExtensionContext context) throws Exception {
3438
TestEngine.initTest(testClass);
3539
}
3640

37-
3841
@Override
3942
public void beforeEach(ExtensionContext context) throws Exception {
4043
if (useCustomRunner) {
4144
if (!Play.started) {
4245
Play.forceProd = true;
4346
Play.init(new File("."), getPlayId());
4447
}
48+
// Store invocation context in ThreadLocal for use in interceptTestMethod
49+
invocationContext.set(new Invoker.InvocationContext(invocationType));
50+
}
51+
}
52+
53+
@Override
54+
public void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
55+
if (useCustomRunner) {
56+
// Run the test method within the Play thread context
57+
Invoker.InvocationContext ctx = new Invoker.InvocationContext(invocationType);
58+
Throwable[] testException = new Throwable[1];
59+
4560
Invoker.invokeInThread(new DirectInvocation() {
4661
@Override
4762
public void execute() throws Exception {
48-
// Nothing here; test method will run
63+
try {
64+
invocation.proceed();
65+
} catch (Throwable e) {
66+
testException[0] = e;
67+
}
4968
}
69+
5070
@Override
5171
public Invoker.InvocationContext getInvocationContext() {
52-
return new Invoker.InvocationContext(invocationType);
72+
return ctx;
5373
}
5474
});
75+
76+
if (testException[0] != null) {
77+
throw testException[0];
78+
}
79+
} else {
80+
invocation.proceed();
5581
}
5682
}
5783

0 commit comments

Comments
 (0)