gpt4 book ai didi

java - 运行 junit 测试用例时,ExceptionMapper toResponse 未调用。但在通过 Client/Postman 运行时调用

转载 作者:太空宇宙 更新时间:2023-11-04 11:15:52 25 4
gpt4 key购买 nike

我有一个测试用例,在进行一些基本验证时抛出异常。但 ExceptionMapper 没有被调用。但如果我从 postman 那里跑去使用服务,它就工作正常。Junit 测试必须以不同的方式运行 ExceptionMapper 吗?

测试用例:

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

}

Controller :

@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();
}

}

异常(exception):

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

最佳答案

您认为为什么应该调用异常映射器?这不是集成测试。您所做的就是实例化该类,然后调用一个方法。 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);
}
}

关于java - 运行 junit 测试用例时,ExceptionMapper toResponse 未调用。但在通过 Client/Postman 运行时调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45466435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com