2

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?

Fabrizio Stellato
  • 1,727
  • 21
  • 52

2 Answers2

3

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

Fabrizio Stellato
  • 1,727
  • 21
  • 52
1

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.

Jonathan S. Fisher
  • 8,189
  • 6
  • 46
  • 84
  • Sorry, I tried but there's something missing, it says Couldn't find an instantiated field on your Test Case annotated with @InjectMocks. I tried to apply on HttpServletRequest servletRequest, but it doesn't work since is an interface – Fabrizio Stellato Feb 14 '20 at 11:01
  • ConfigApiResource would have to be annotated @InjectMocks, and every bean it uses be mocks. Depending on your intention (Are you doing an integration test, or true unit test?) this may be desirable. I also had a second thought... can you make a `@Producer` bean for the request object and include it in your Weld config? – Jonathan S. Fisher Feb 14 '20 at 15:49