Skip to content

Commit 6112274

Browse files
committed
Prevent simultaneous use of @InjectMock and @InjectSpy
Closes: #50163
1 parent 10147ea commit 6112274

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

test-framework/junit5-mockito/src/main/java/io/quarkus/test/junit/mockito/internal/CreateMockitoSpiesCallback.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import io.quarkus.arc.ClientProxy;
1313
import io.quarkus.arc.InjectableContext;
1414
import io.quarkus.arc.InstanceHandle;
15+
import io.quarkus.test.InjectMock;
1516
import io.quarkus.test.junit.callback.QuarkusTestAfterAllCallback;
1617
import io.quarkus.test.junit.callback.QuarkusTestAfterConstructCallback;
1718
import io.quarkus.test.junit.callback.QuarkusTestContext;
@@ -35,7 +36,14 @@ public void afterConstruct(Object testInstance) {
3536
while (current.getSuperclass() != null) {
3637
for (Field field : current.getDeclaredFields()) {
3738
InjectSpy injectSpyAnnotation = field.getAnnotation(InjectSpy.class);
39+
InjectMock injectMockAnnotation = field.getAnnotation(InjectMock.class);
3840
if (injectSpyAnnotation != null) {
41+
if (injectMockAnnotation != null) {
42+
cleanup();
43+
throw new IllegalArgumentException(
44+
"Fields cannot be annotated with both @InjectSpy and @InjectMock. Offending field is '"
45+
+ field.getDeclaringClass().getName() + "#" + field.getName() + "'");
46+
}
3947
InstanceHandle<?> beanHandle = CreateMockitoMocksCallback.getBeanHandle(testInstance, field,
4048
InjectSpy.class);
4149
Object spy = createSpyAndSetTestField(testInstance, field, beanHandle,
@@ -72,6 +80,10 @@ private Object createSpyAndSetTestField(Object testInstance, Field field, Instan
7280

7381
@Override
7482
public void afterAll(QuarkusTestContext context) {
83+
cleanup();
84+
}
85+
86+
private static void cleanup() {
7587
if (!statesToDestroy.isEmpty()) {
7688
for (InjectableContext.ContextState state : statesToDestroy) {
7789
Arc.container().requestContext().destroy(state);

0 commit comments

Comments
 (0)