I have a scenario where ExceptionMapper
are used in JAX-RS using RESTeasy 2.0.1.GA . This works all fine.
I'd now like to test the whole thing using RESTeasy's mock mechanism. Unfortunately my ExceptionMapper-provider is not registered. What am I missing?
POJOResourceFactory factory = new POJOResourceFactory(SomeWebResource.class);
Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
dispatcher.getRegistry().addResourceFactory(factory);
MockHttpRequest request = MockHttpRequest.get("url");
MockHttpResponse response = new MockHttpResponse();
// here my exception is thrown
dispatcher.invoke(request, response);
// but I expect the response to be 404 (which works outside the mock setup)
Assert.assertEquals(response.getStatus(), 404);
Okay, I found the solution. Needed to register the ExceptionMapper manually:
dispatcher.getProviderFactory().addExceptionMapper(SomeExceptionMapper.class);
After struggling with this problem for a few days now, I think it's worth mentioning what @Joe W wrote in the comment of the above answer as it's own answer:
"Note: addExceptionMapper()
's visibility was changed to protected in later versions. dispatcher.getProviderFactory().registerProvider(SomeExceptionMapper.class)
works instead."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With