Skip to content

Commit

Permalink
Don't need to @Inject the @MockBean
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <[email protected]>
  • Loading branch information
jbescos committed Apr 22, 2024
1 parent 0d67a63 commit 319343b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.context.RequestScoped;
import jakarta.enterprise.event.Observes;
import jakarta.enterprise.inject.Any;
import jakarta.enterprise.inject.se.SeContainer;
import jakarta.enterprise.inject.se.SeContainerInitializer;
import jakarta.enterprise.inject.spi.AfterBeanDiscovery;
Expand Down Expand Up @@ -545,6 +546,8 @@ void processMockBean(@Observes @WithAnnotations(MockBean.class) ProcessAnnotated
MockBean mockBean = field.getAnnotated().getAnnotation(MockBean.class);
if (mockBean != null) {
Field f = field.getAnnotated().getJavaMember();
// Adds @Inject if not found, so it is more user friendly
field.add(MockBean.Literal.INSTANCE);
Class<?> fieldType = f.getType();
mocks.add(fieldType);
}
Expand Down Expand Up @@ -575,7 +578,8 @@ void registerOtherBeans(@Observes AfterBeanDiscovery event) {
event.addBean()
.addType(type)
.scope(ApplicationScoped.class)
.createWith(inst -> Mockito.mock(type));
.createWith(inst -> Mockito.mock(type))
.priority(0);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,25 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.inject.Inject;

/**
* A field annotated with @MockBean will be mocked by Mockito
* and injected in every place it is referenced.
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
public @interface MockBean {

/**
* Supports inline instantiation of the {@link Inject} annotation.
*/
final static class Literal extends AnnotationLiteral<Inject> implements Inject {

public static final Literal INSTANCE = new Literal();

private static final long serialVersionUID = 1L;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@
@HelidonTest
@AddBean(TestMockBean.Resource.class)
//@AddBean(TestMockBean.Service.class)
//@AddBean(TestMockBean.OtherService.class)
public class TestMockBean {

@Inject
// Without @Inject
@MockBean
private Service service;
// With @Inject
@Inject
@MockBean
private OtherService otherService;
@Inject
private WebTarget target;

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

@Path("/test")
Expand All @@ -68,4 +75,12 @@ public String test() {
}

}

public static class OtherService {

public String test() {
return "OtherService";
}

}
}

0 comments on commit 319343b

Please sign in to comment.