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?
After asking help also to weld-junit group, user mkouba gave the final solution.
By adding the observer on Context annotation, the servletRequest mock is properly injected on ConfigApiResource