junit5 + weld + mockito extension, how to inject @Context object

1.6k views Asked by At

This is my REST resource:

@Context
HttpServletRequest webRequest;


@Override
public DomainConfig get() {
    return configDelegate.get(webRequest.getHeader("Origin"));
}

I've built my unit test with Junit 5 + Weld / Mockito extension.

@MockitoSettings(strictness = Strictness.STRICT_STUBS)
@ExtendWith(MockitoExtension.class)
@DisplayName("configs resource")
@EnableWeld
public class ConfigApiResourceTest {

    @Mock
    HttpServletRequest servletRequest;

    @WeldSetup
    public WeldInitiator weld = WeldInitiator
            .from(
                    MockCommonResources.class,
                    ConfigApiResource.class,
                    ConfigDelegate.class,
                    ConfigService.class,
                    etc etc
            )
            .addBeans(createHttpServletRequest())
            .activate(
                    RequestScoped.class,
                    ApplicationScoped.class
            )
            .build();

    Bean<?> createHttpServletRequest() {
        return MockBean.builder()
                .types(HttpServletRequest.class)
                .create(o -> servletRequest)
                .build();
    }

    @Test
    @DisplayName("config")
    void config(ConfigApiResource configApiResource) {

        final String url = "areaclient.infocert.it";

        when(servletRequest.getHeader("Origin")).thenReturn(url);

        final DomainConfig output = configApiResource.get();
        assertNotNull(output);

    }
}

The issue is HttpServletRequest webRequest is always null, probably because is not injected but it is a context object.

So the real question is, how can I produce a HttpServletRequest mock and inject as a @Context object?

2

There are 2 answers

0
Fabrizio Stellato On BEST ANSWER

After asking help also to weld-junit group, user mkouba gave the final solution.

final Weld weldBase = WeldInitiator.createWeld()
            .addBeanClasses(
                    MockCommonResources.class,
                    ConfigApiResource.class,
                    ConfigDelegate.class,
                    ConfigService.class,
                    etc etc
            )
            .addContainerLifecycleObserver(ContainerLifecycleObserver.processAnnotatedType()
                    .notify(pat -> pat.configureAnnotatedType()
                            .filterFields(m -> m.isAnnotationPresent(Context.class))
                            .forEach(m -> m.add(javax.enterprise.inject.literal.InjectLiteral.INSTANCE))));


    @WeldSetup
    public WeldInitiator weld = WeldInitiator
            .from(
                    weldBase
            )
            .activate(
                    RequestScoped.class,
                    ApplicationScoped.class
            )
            .addBeans(createHttpServletRequest())
            .build();

By adding the observer on Context annotation, the servletRequest mock is properly injected on ConfigApiResource

2
Jonathan S. Fisher On

We had this problem trying to @Inject private Boolean securityEnabled; objects, because Boolean cannot be mocked. We solved it with a custom JUnit/Mockito extension: https://github.com/exabrial/mockito-object-injection

 @InjectionMap
 private Map<String, Object> injectionMap = new HashMap<>();

 @BeforeEach
 public void beforeEach() throws Exception {
  injectionMap.put("securityEnabled", Boolean.TRUE);
 }

 @AfterEach
 public void afterEach() throws Exception {
  injectionMap.clear();
 }

You could do the same by @Mocking HttpServletRequest and Context and then setting them in the injection map.