Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 450
Jan Avatar asked May 03 '11 07:05

Jan


2 Answers

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

dispatcher.getProviderFactory().addExceptionMapper(SomeExceptionMapper.class);
like image 86
Jan Avatar answered Nov 03 '22 14:11

Jan


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."

like image 1
Ben2307 Avatar answered Nov 03 '22 12:11

Ben2307