Skip to content

Commit 319343b

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

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import jakarta.enterprise.context.Dependent;
4343
import jakarta.enterprise.context.RequestScoped;
4444
import jakarta.enterprise.event.Observes;
45+
import jakarta.enterprise.inject.Any;
4546
import jakarta.enterprise.inject.se.SeContainer;
4647
import jakarta.enterprise.inject.se.SeContainerInitializer;
4748
import jakarta.enterprise.inject.spi.AfterBeanDiscovery;
@@ -545,6 +546,8 @@ void processMockBean(@Observes @WithAnnotations(MockBean.class) ProcessAnnotated
545546
MockBean mockBean = field.getAnnotated().getAnnotation(MockBean.class);
546547
if (mockBean != null) {
547548
Field f = field.getAnnotated().getJavaMember();
549+
// Adds @Inject if not found, so it is more user friendly
550+
field.add(MockBean.Literal.INSTANCE);
548551
Class<?> fieldType = f.getType();
549552
mocks.add(fieldType);
550553
}
@@ -575,7 +578,8 @@ void registerOtherBeans(@Observes AfterBeanDiscovery event) {
575578
event.addBean()
576579
.addType(type)
577580
.scope(ApplicationScoped.class)
578-
.createWith(inst -> Mockito.mock(type));
581+
.createWith(inst -> Mockito.mock(type))
582+
.priority(0);
579583
});
580584
}
581585

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,25 @@
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
2323

24+
import jakarta.enterprise.util.AnnotationLiteral;
25+
import jakarta.inject.Inject;
26+
2427
/**
2528
* A field annotated with @MockBean will be mocked by Mockito
2629
* and injected in every place it is referenced.
2730
*/
28-
@Target({ElementType.FIELD})
2931
@Retention(RetentionPolicy.RUNTIME)
32+
@Target({ElementType.FIELD, ElementType.PARAMETER})
3033
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+
}
3145
}

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)