ExceptionMapper toResponse在运行junit测试用例时不调用。但是在通过Client / Postman运行时调用

时间:2017-08-02 16:37:31

标签: java jersey jax-rs junit4 jersey-2.0

我有一个测试用例,其中包含一些基本验证的异常。但是没有调用ExceptionMapper。但如果我从邮递员运行到服务,它工作正常。 对于ExceptionMapper,Junit测试必须以不同的方式运行吗?

测试用例

@Test
    public void itShouldHavePersonNumber() {
RestAuthController controller = new RestAuthController();
    Response response = controller.insertGuid(null, "m012");
    assertThatExceptionOfType(ValidationException.class).isThrownBy(() -> {controller.insertGuid(null, "m012");});
    assertThat(response.getStatus()).isEqualTo(Status.BAD_REQUEST.getStatusCode());

}

控制器:

@POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response insertGuid(@QueryParam("personNumber") Integer personNumber, @QueryParam("guId") String guId ) throws ValidationException {


        if(guId == null || guId.isEmpty()) {
            throw new ValidationException("guId is Required");
        }
}

异常映射器

@Provider
public class ValidationMapper implements ExceptionMapper<ValidationException> {

    @Override
    public Response toResponse(ValidationException ex) {
        return Response.status(Response.Status.BAD_REQUEST).entity(ex.getMessage()).type(MediaType.TEXT_PLAIN).build();
    }

}

例外:

    public class ValidationException extends Exception {

        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        public ValidationException() {
            super();
        }


        public ValidationException(String message, Throwable cause) {
            super(message, cause);
        }

        public ValidationException(String message) {
            super(message);
        }
}

1 个答案:

答案 0 :(得分:0)

为什么你认为应该调用异常映射器?它不是集成测试。您所做的只是实例化类,然后调用方法。 Java中没有什么神奇之处可以调用异常映射器。如果要调用映射器,则需要运行与应用程序运行的Jersey应用程序(以及已注册的映射器)的集成测试。

与Jersey运行集成测试的一种方法是使用它Test Framework。以下是一个例子。

public class ValidationExceptionTest extends JerseyTest {

    public static class ValidationException extends RuntimeException {}

    public static class ValidationExceptionMapper implements ExceptionMapper<ValidationException> {
        @Override
        public Response toResponse(ValidationException e) {
            return Response.status(400).entity("boo boo").build();
        }
    }

    @Path("echo-name")
    public static class EchoNameResource {
        @GET
        public String echoName(@QueryParam("name") String name) {
            if (name == null || name.isEmpty()) {
                throw new ValidationException();
            }
            return name;
        }
    }

    @Override
    public ResourceConfig configure() {
        return new ResourceConfig()
                .register(EchoNameResource.class)
                .register(ValidationExceptionMapper.class);
    }

    @Test
    public void testResponseOkWithQueryParam() {
        final Response response = target("echo-name")
                .queryParam("name", "peeskillet")
                .request()
                .get();

        assertThat(response.getStatus()).isEqualTo(200);
        assertThat(response.readEntity(String.class)).isEqualTo("peeskillet");
    }

    @Test
    public void testResponseBadRequestWithNoQueryParam() {
        final Response response = target("echo-name")
                .request()
                .get();

        assertThat(response.getStatus()).isEqualTo(400);
    }
}