12
12
import io .quarkus .arc .ClientProxy ;
13
13
import io .quarkus .arc .InjectableContext ;
14
14
import io .quarkus .arc .InstanceHandle ;
15
+ import io .quarkus .test .InjectMock ;
15
16
import io .quarkus .test .junit .callback .QuarkusTestAfterAllCallback ;
16
17
import io .quarkus .test .junit .callback .QuarkusTestAfterConstructCallback ;
17
18
import io .quarkus .test .junit .callback .QuarkusTestContext ;
@@ -21,7 +22,7 @@ public class CreateMockitoSpiesCallback implements QuarkusTestAfterConstructCall
21
22
22
23
// Set is here because in nested tests, there are multiple states created before destruction is triggered
23
24
// This field needs to be static because each implemented callback created a new instance of this class
24
- private static Set <InjectableContext .ContextState > statesToDestroy = new HashSet <>();
25
+ private static final Set <InjectableContext .ContextState > statesToDestroy = new HashSet <>();
25
26
26
27
@ Override
27
28
public void afterConstruct (Object testInstance ) {
@@ -35,7 +36,14 @@ public void afterConstruct(Object testInstance) {
35
36
while (current .getSuperclass () != null ) {
36
37
for (Field field : current .getDeclaredFields ()) {
37
38
InjectSpy injectSpyAnnotation = field .getAnnotation (InjectSpy .class );
39
+ InjectMock injectMockAnnotation = field .getAnnotation (InjectMock .class );
38
40
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
+ }
39
47
InstanceHandle <?> beanHandle = CreateMockitoMocksCallback .getBeanHandle (testInstance , field ,
40
48
InjectSpy .class );
41
49
Object spy = createSpyAndSetTestField (testInstance , field , beanHandle ,
@@ -72,6 +80,10 @@ private Object createSpyAndSetTestField(Object testInstance, Field field, Instan
72
80
73
81
@ Override
74
82
public void afterAll (QuarkusTestContext context ) {
83
+ cleanup ();
84
+ }
85
+
86
+ private static void cleanup () {
75
87
if (!statesToDestroy .isEmpty ()) {
76
88
for (InjectableContext .ContextState state : statesToDestroy ) {
77
89
Arc .container ().requestContext ().destroy (state );
0 commit comments