球衣测试-不调用ExceptionMapper [英] Jersey test - ExceptionMapper is not invoked

查看:214
本文介绍了球衣测试-不调用ExceptionMapper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有REST Web服务,可以使用Jersey Test,Mockito和Junit进行测试.成功执行Web服务方法后,我将获得正确的响应.如果数据无效,则引发自定义异常,该异常必须由ExceptionMapper处理.它应该返回相同的结构响应,但使用不同的代码. ExceptionMapper在非测试环境中运行良好.但是,测试执行后的日志显示:

I have REST web service which I test with Jersey Test, Mockito, Junit. When web service method is executed successfully, I get correct response. In case of invalid data, custom exception is thrown which must be handled by ExceptionMapper. It should return the same structure response but with different codes. ExceptionMapper works well in not test environment. However, after test execution logs show:

1 < 500
1 < Connection: close
1 < Content-Length: 1033
1 < Content-Type: text/html;charset=ISO-8859-1
1 < Date: Wed, 30 Mar 2016 11:55:37 GMT

javax.ws.rs.InternalServerErrorException: HTTP 500 Request failed.
at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:1020)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:816)
at org.glassfish.jersey.client.JerseyInvocation.access$700(JerseyInvocation.java:92)

我使用:

<dependency>
    <groupId>org.glassfish.jersey.test-framework.providers</groupId>
    <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
    <version>2.22.2</version>
</dependency>

受测试类扩展的模拟服务的简化版本:

Shortened version of mocked service which is extented by test classes:

public class MockedService extends JerseyTest {

    @Override
    public ResourceConfig configure() {

        forceSet(TestProperties.CONTAINER_PORT, "8080");
        enable(TestProperties.LOG_TRAFFIC);

        return new ResourceConfig().register(new Service());
    }
}

如何从ExceptionMapper获得响应?

How to get response from ExceptionMapper?

推荐答案

您仍然需要注册ExceptionMapper

return new ResourceConfig()
        .register(new Service())
        .register(new YourMapper());

在您的实际环境中,可能会使用包扫描或类路径扫描来扫描映射器.

In you real environment, the mapper is probably scanned for, either with package scanning or classpath scanning.

这篇关于球衣测试-不调用ExceptionMapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆