File tree 3 files changed +36
-3
lines changed
testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5
tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5
3 files changed +36
-3
lines changed Original file line number Diff line number Diff line change 42
42
import jakarta .enterprise .context .Dependent ;
43
43
import jakarta .enterprise .context .RequestScoped ;
44
44
import jakarta .enterprise .event .Observes ;
45
+ import jakarta .enterprise .inject .Any ;
45
46
import jakarta .enterprise .inject .se .SeContainer ;
46
47
import jakarta .enterprise .inject .se .SeContainerInitializer ;
47
48
import jakarta .enterprise .inject .spi .AfterBeanDiscovery ;
@@ -545,6 +546,8 @@ void processMockBean(@Observes @WithAnnotations(MockBean.class) ProcessAnnotated
545
546
MockBean mockBean = field .getAnnotated ().getAnnotation (MockBean .class );
546
547
if (mockBean != null ) {
547
548
Field f = field .getAnnotated ().getJavaMember ();
549
+ // Adds @Inject if not found, so it is more user friendly
550
+ field .add (MockBean .Literal .INSTANCE );
548
551
Class <?> fieldType = f .getType ();
549
552
mocks .add (fieldType );
550
553
}
@@ -575,7 +578,8 @@ void registerOtherBeans(@Observes AfterBeanDiscovery event) {
575
578
event .addBean ()
576
579
.addType (type )
577
580
.scope (ApplicationScoped .class )
578
- .createWith (inst -> Mockito .mock (type ));
581
+ .createWith (inst -> Mockito .mock (type ))
582
+ .priority (0 );
579
583
});
580
584
}
581
585
Original file line number Diff line number Diff line change 21
21
import java .lang .annotation .RetentionPolicy ;
22
22
import java .lang .annotation .Target ;
23
23
24
+ import jakarta .enterprise .util .AnnotationLiteral ;
25
+ import jakarta .inject .Inject ;
26
+
24
27
/**
25
28
* A field annotated with @MockBean will be mocked by Mockito
26
29
* and injected in every place it is referenced.
27
30
*/
28
- @ Target ({ElementType .FIELD })
29
31
@ Retention (RetentionPolicy .RUNTIME )
32
+ @ Target ({ElementType .FIELD , ElementType .PARAMETER })
30
33
public @interface MockBean {
34
+
35
+ /**
36
+ * Supports inline instantiation of the {@link Inject} annotation.
37
+ */
38
+ final static class Literal extends AnnotationLiteral <Inject > implements Inject {
39
+
40
+ public static final Literal INSTANCE = new Literal ();
41
+
42
+ private static final long serialVersionUID = 1L ;
43
+
44
+ }
31
45
}
Original file line number Diff line number Diff line change 34
34
@ HelidonTest
35
35
@ AddBean (TestMockBean .Resource .class )
36
36
//@AddBean(TestMockBean.Service.class)
37
+ //@AddBean(TestMockBean.OtherService.class)
37
38
public class TestMockBean {
38
39
39
- @ Inject
40
+ // Without @Inject
40
41
@ MockBean
41
42
private Service service ;
43
+ // With @Inject
44
+ @ Inject
45
+ @ MockBean
46
+ private OtherService otherService ;
42
47
@ Inject
43
48
private WebTarget target ;
44
49
@@ -47,6 +52,8 @@ public void injectionTest() {
47
52
Mockito .when (service .test ()).thenReturn ("Mocked" );
48
53
String response = target .path ("/test" ).request ().get (String .class );
49
54
assertThat (response , is ("Mocked" ));
55
+ Mockito .when (otherService .test ()).thenReturn ("Mocked" );
56
+ assertThat (otherService .test (), is ("Mocked" ));
50
57
}
51
58
52
59
@ Path ("/test" )
@@ -68,4 +75,12 @@ public String test() {
68
75
}
69
76
70
77
}
78
+
79
+ public static class OtherService {
80
+
81
+ public String test () {
82
+ return "OtherService" ;
83
+ }
84
+
85
+ }
71
86
}
You can’t perform that action at this time.
0 commit comments