Search code examples
unit-testingmockingjax-rsresteasy

RESTeasy + Server mock + ExceptionMapper not found


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);

Solution

  • Okay, I found the solution. Needed to register the ExceptionMapper manually:

    dispatcher.getProviderFactory().addExceptionMapper(SomeExceptionMapper.class);