File tree 3 files changed +33
-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 +33
-3
lines changed Original file line number Diff line number Diff line change @@ -545,6 +545,8 @@ void processMockBean(@Observes @WithAnnotations(MockBean.class) ProcessAnnotated
545
545
MockBean mockBean = field .getAnnotated ().getAnnotation (MockBean .class );
546
546
if (mockBean != null ) {
547
547
Field f = field .getAnnotated ().getJavaMember ();
548
+ // Adds @Inject if not found, so it is more user friendly
549
+ field .add (Literal .INSTANCE );
548
550
Class <?> fieldType = f .getType ();
549
551
mocks .add (fieldType );
550
552
}
@@ -575,7 +577,8 @@ void registerOtherBeans(@Observes AfterBeanDiscovery event) {
575
577
event .addBean ()
576
578
.addType (type )
577
579
.scope (ApplicationScoped .class )
578
- .createWith (inst -> Mockito .mock (type ));
580
+ .createWith (inst -> Mockito .mock (type ))
581
+ .priority (0 );
579
582
});
580
583
}
581
584
@@ -765,4 +768,15 @@ public Class<? extends Extension> value() {
765
768
}
766
769
}
767
770
771
+ /**
772
+ * Supports inline instantiation of the {@link Inject} annotation.
773
+ */
774
+ private static final class Literal extends AnnotationLiteral <Inject > implements Inject {
775
+
776
+ private static final Literal INSTANCE = new Literal ();
777
+
778
+ @ Serial
779
+ private static final long serialVersionUID = 1L ;
780
+
781
+ }
768
782
}
Original file line number Diff line number Diff line change 25
25
* A field annotated with @MockBean will be mocked by Mockito
26
26
* and injected in every place it is referenced.
27
27
*/
28
- @ Target ({ElementType .FIELD })
29
28
@ Retention (RetentionPolicy .RUNTIME )
29
+ @ Target ({ElementType .FIELD , ElementType .PARAMETER })
30
30
public @interface MockBean {
31
+
31
32
}
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