Skip to content

Commit 8aa9531

Browse files
committed
Don't need to @Inject the @MockBean
Signed-off-by: Jorge Bescos Gascon <[email protected]>
1 parent 0d67a63 commit 8aa9531

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,8 @@ void processMockBean(@Observes @WithAnnotations(MockBean.class) ProcessAnnotated
545545
MockBean mockBean = field.getAnnotated().getAnnotation(MockBean.class);
546546
if (mockBean != null) {
547547
Field f = field.getAnnotated().getJavaMember();
548+
// Adds @Inject if not found, so it is more user friendly
549+
field.add(Literal.INSTANCE);
548550
Class<?> fieldType = f.getType();
549551
mocks.add(fieldType);
550552
}
@@ -575,7 +577,8 @@ void registerOtherBeans(@Observes AfterBeanDiscovery event) {
575577
event.addBean()
576578
.addType(type)
577579
.scope(ApplicationScoped.class)
578-
.createWith(inst -> Mockito.mock(type));
580+
.createWith(inst -> Mockito.mock(type))
581+
.priority(0);
579582
});
580583
}
581584

@@ -765,4 +768,15 @@ public Class<? extends Extension> value() {
765768
}
766769
}
767770

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+
}
768782
}

microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/MockBean.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
* A field annotated with @MockBean will be mocked by Mockito
2626
* and injected in every place it is referenced.
2727
*/
28-
@Target({ElementType.FIELD})
2928
@Retention(RetentionPolicy.RUNTIME)
29+
@Target({ElementType.FIELD, ElementType.PARAMETER})
3030
public @interface MockBean {
31+
3132
}

microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestMockBean.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,16 @@
3434
@HelidonTest
3535
@AddBean(TestMockBean.Resource.class)
3636
//@AddBean(TestMockBean.Service.class)
37+
//@AddBean(TestMockBean.OtherService.class)
3738
public class TestMockBean {
3839

39-
@Inject
40+
// Without @Inject
4041
@MockBean
4142
private Service service;
43+
// With @Inject
44+
@Inject
45+
@MockBean
46+
private OtherService otherService;
4247
@Inject
4348
private WebTarget target;
4449

@@ -47,6 +52,8 @@ public void injectionTest() {
4752
Mockito.when(service.test()).thenReturn("Mocked");
4853
String response = target.path("/test").request().get(String.class);
4954
assertThat(response, is("Mocked"));
55+
Mockito.when(otherService.test()).thenReturn("Mocked");
56+
assertThat(otherService.test(), is("Mocked"));
5057
}
5158

5259
@Path("/test")
@@ -68,4 +75,12 @@ public String test() {
6875
}
6976

7077
}
78+
79+
public static class OtherService {
80+
81+
public String test() {
82+
return "OtherService";
83+
}
84+
85+
}
7186
}

0 commit comments

Comments
 (0)