RESTeasy + Server模拟+未找到ExceptionMapper

时间:2011-05-03 07:57:40

标签: unit-testing mocking jax-rs resteasy

我有一个使用RESTeasy 2.0.1.GA在JAX-RS中使用ExceptionMapper的场景。这一切都很好。

我现在想用RESTeasy的mock mechanism来测试整个事情。不幸的是我的ExceptionMapper-provider没有注册。我错过了什么?

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

2 个答案:

答案 0 :(得分:12)

好的,我找到了解决方案。需要手动注册ExceptionMapper:

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

答案 1 :(得分:1)

在经历了几天这个问题的挣扎之后,我认为值得一提的是@Joe W在上述答案的评论中所写的内容是它自己的答案:

"注意:addExceptionMapper()的可见性在以后的版本中已更改为受保护。 dispatcher.getProviderFactory().registerProvider(SomeExceptionMapper.class)可以改为工作。"