Skip to content

Commit

Permalink
Merge pull request #9815 from geoand/#9812
Browse files Browse the repository at this point in the history
Introduce more context into test callbacks
  • Loading branch information
geoand authored Jun 10, 2020
2 parents bdbbf50 + 0502a39 commit 95f012b
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import io.quarkus.panache.mock.PanacheMock;
import io.quarkus.test.junit.callback.QuarkusTestAfterEachCallback;
import io.quarkus.test.junit.callback.QuarkusTestMethodContext;

public class PanacheMockAfterEachTest implements QuarkusTestAfterEachCallback {

@Override
public void afterEach(Object testInstance) {
public void afterEach(QuarkusTestMethodContext context) {
PanacheMock.reset();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package io.quarkus.test.junit.mockito.internal;

import io.quarkus.test.junit.callback.QuarkusTestAfterEachCallback;
import io.quarkus.test.junit.callback.QuarkusTestMethodContext;

public class ResetMockitoMocksCallback implements QuarkusTestAfterEachCallback {

@Override
public void afterEach(Object testInstance) {
MockitoMocksTracker.reset(testInstance);
public void afterEach(QuarkusTestMethodContext context) {
MockitoMocksTracker.reset(context.getTestInstance());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

import io.quarkus.test.junit.QuarkusMock;
import io.quarkus.test.junit.callback.QuarkusTestBeforeEachCallback;
import io.quarkus.test.junit.callback.QuarkusTestMethodContext;

public class SetMockitoMockAsBeanMockCallback implements QuarkusTestBeforeEachCallback {

@Override
public void beforeEach(Object testInstance) {
MockitoMocksTracker.getMocks(testInstance).forEach(this::installMock);
public void beforeEach(QuarkusTestMethodContext context) {
MockitoMocksTracker.getMocks(context.getTestInstance()).forEach(this::installMock);
}

private void installMock(MockitoMocksTracker.Mocked mocked) {
QuarkusMock.installMockForInstance(mocked.mock, mocked.beanInstance);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -64,6 +65,7 @@
import io.quarkus.test.junit.callback.QuarkusTestAfterEachCallback;
import io.quarkus.test.junit.callback.QuarkusTestBeforeAllCallback;
import io.quarkus.test.junit.callback.QuarkusTestBeforeEachCallback;
import io.quarkus.test.junit.callback.QuarkusTestMethodContext;
import io.quarkus.test.junit.internal.DeepClone;
import io.quarkus.test.junit.internal.XStreamDeepClone;

Expand All @@ -87,6 +89,7 @@ public class QuarkusTestExtension
private static List<Object> beforeAllCallbacks = new ArrayList<>();
private static List<Object> beforeEachCallbacks = new ArrayList<>();
private static List<Object> afterEachCallbacks = new ArrayList<>();
private static Class<?> quarkusTestMethodContextClass;

private static DeepClone deepClone;

Expand Down Expand Up @@ -231,33 +234,6 @@ private void populateCallbacks(ClassLoader classLoader) throws ClassNotFoundExce
}
}

@Override
public void afterEach(ExtensionContext context) throws Exception {
if (isNativeTest(context)) {
return;
}
if (!failedBoot) {
popMockContext();
for (Object afterEachCallback : afterEachCallbacks) {
afterEachCallback.getClass().getMethod("afterEach", Object.class).invoke(afterEachCallback, actualTestInstance);
}
boolean nativeImageTest = isNativeTest(context);
ClassLoader original = setCCL(runningQuarkusApplication.getClassLoader());
try {
runningQuarkusApplication.getClassLoader().loadClass(RestAssuredURLManager.class.getName())
.getDeclaredMethod("clearURL").invoke(null);
runningQuarkusApplication.getClassLoader().loadClass(TestScopeManager.class.getName())
.getDeclaredMethod("tearDown", boolean.class).invoke(null, nativeImageTest);
} finally {
setCCL(original);
}
}
}

private boolean isNativeTest(ExtensionContext context) {
return context.getRequiredTestClass().isAnnotationPresent(NativeImageTest.class);
}

@Override
public void beforeEach(ExtensionContext context) throws Exception {
if (isNativeTest(context)) {
Expand All @@ -268,15 +244,15 @@ public void beforeEach(ExtensionContext context) throws Exception {
try {
pushMockContext();
for (Object beforeEachCallback : beforeEachCallbacks) {
beforeEachCallback.getClass().getMethod("beforeEach", Object.class).invoke(beforeEachCallback,
actualTestInstance);
Map.Entry<Class<?>, ?> tuple = createQuarkusTestMethodContextTuple(context);
beforeEachCallback.getClass().getMethod("beforeEach", tuple.getKey())
.invoke(beforeEachCallback, tuple.getValue());
}
boolean nativeImageTest = isNativeTest(context);
if (runningQuarkusApplication != null) {
runningQuarkusApplication.getClassLoader().loadClass(RestAssuredURLManager.class.getName())
.getDeclaredMethod("setURL", boolean.class).invoke(null, false);
runningQuarkusApplication.getClassLoader().loadClass(TestScopeManager.class.getName())
.getDeclaredMethod("setup", boolean.class).invoke(null, nativeImageTest);
.getDeclaredMethod("setup", boolean.class).invoke(null, false);
}
} finally {
setCCL(original);
Expand All @@ -292,6 +268,44 @@ public void beforeEach(ExtensionContext context) throws Exception {
}
}

@Override
public void afterEach(ExtensionContext context) throws Exception {
if (isNativeTest(context)) {
return;
}
if (!failedBoot) {
popMockContext();
for (Object afterEachCallback : afterEachCallbacks) {
Map.Entry<Class<?>, ?> tuple = createQuarkusTestMethodContextTuple(context);
afterEachCallback.getClass().getMethod("afterEach", tuple.getKey())
.invoke(afterEachCallback, tuple.getValue());
}
ClassLoader original = setCCL(runningQuarkusApplication.getClassLoader());
try {
runningQuarkusApplication.getClassLoader().loadClass(RestAssuredURLManager.class.getName())
.getDeclaredMethod("clearURL").invoke(null);
runningQuarkusApplication.getClassLoader().loadClass(TestScopeManager.class.getName())
.getDeclaredMethod("tearDown", boolean.class).invoke(null, false);
} finally {
setCCL(original);
}
}
}

private Map.Entry<Class<?>, ?> createQuarkusTestMethodContextTuple(ExtensionContext context) throws Exception {
if (quarkusTestMethodContextClass == null) {
quarkusTestMethodContextClass = Class.forName(QuarkusTestMethodContext.class.getName(), true,
runningQuarkusApplication.getClassLoader());
}
Constructor<?> constructor = quarkusTestMethodContextClass.getConstructor(Object.class, Method.class);
return new AbstractMap.SimpleEntry<>(quarkusTestMethodContextClass,
constructor.newInstance(actualTestInstance, context.getRequiredTestMethod()));
}

private boolean isNativeTest(ExtensionContext context) {
return context.getRequiredTestClass().isAnnotationPresent(NativeImageTest.class);
}

private ExtensionState ensureStarted(ExtensionContext extensionContext) {
ExtensionContext root = extensionContext.getRoot();
ExtensionContext.Store store = root.getStore(ExtensionContext.Namespace.GLOBAL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public interface QuarkusTestAfterEachCallback {

void afterEach(Object testInstance);
void afterEach(QuarkusTestMethodContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public interface QuarkusTestBeforeEachCallback {

void beforeEach(Object testInstance);
void beforeEach(QuarkusTestMethodContext context);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.quarkus.test.junit.callback;

import java.lang.reflect.Method;

/**
* Context object passed to {@link QuarkusTestBeforeEachCallback} and {@link QuarkusTestAfterEachCallback}
*/
public final class QuarkusTestMethodContext {

private final Object testInstance;
private final Method testMethod;

public QuarkusTestMethodContext(Object testInstance, Method testMethod) {
this.testInstance = testInstance;
this.testMethod = testMethod;
}

public Object getTestInstance() {
return testInstance;
}

public Method getTestMethod() {
return testMethod;
}
}

0 comments on commit 95f012b

Please sign in to comment.