I'm getting Providers
from context in my filter to get defined ObjectMapper
public class Filter implements ContainerRequestFilter, ContainerResponseFilter {
@Context
private Providers providers;
@Context
private HttpServletRequest request;
private ObjectMapper getObjectMapper() {
ContextResolver<ObjectMapper> contextResolver = providers.getContextResolver(ObjectMapper.class, MediaType.APPLICATION_JSON_TYPE);
if (contextResolver == null) {
return new ObjectMapper();
}
return contextResolver.getContext(null);
}
}
but in test I can't inject mock in this filter using abstract binder with HttpServletRequest
it works fine but Providers
isn't mock. Example of test:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "..." })
@PrepareForTest({ ... })
public class Test extends JerseyTest {
@Rule
public PowerMockRule rule = new PowerMockRule();
private HttpServletRequest request;
private Providers providers;
@Override
protected Application configure() {
ResourceConfig config = new ResourceConfig(TestResource.class, Filter.class);
providers = mock(Providers.class);
request = mock(HttpServletRequest.class);
config.register(new AbstractBinder() {
@Override
protected void configure() {
bind(providers).to(Providers.class);
}
});
config.register(new AbstractBinder() {
@Override
protected void configure() {
bind(request).to(HttpServletRequest.class);
}
});
return config;
}
Why HttpServletRequest
is mock in filter but Providers
is not?
Providers shouldn't have to be mocked. It is handled by the framework. Any providers you want added, just register with the
ResourceConfig
. I don't know what you care doing wrong in your attempt at this, but below is a complete working example where theContextResolver
is discovered just fine.If you still can't figure it out, please provide a full working single class example (without any mock or Spring stuff) like I have done.