JUnit 测试错误地通过了错误的参数
JUnit Test wrongly pass with wrong parameters
我尝试测试控制器的方法并获得正确的异常。它仅针对 ConstraintViolationException (javax.validation) 失败,而其他测试如 MissingServletRequestParameterException 或 MethodArgumentTypeMismatchException 测试按预期工作。
下面是我的代码的一部分,在控制器 (@Validated) 中,在我添加这些注释的方法中。
还有 ControllerAdvice 和测试。
邮递员,当我输入错误的参数 returns 时,正确的响应是:
{"value":500,"name":"ConstraintViolationException","message":"param1 错误"},但由于某些奇怪的原因,JUnit 要求 200 而不是 500。
控制器
@ResponseBody
public Board method(@Min(value = 0) @Max(value = 5) @RequestParam(value = "param1", required = true) int param1,
@Min(value = 0) @Max(value = 1) @RequestParam(value = "param2", required = true) int param3){
return service.meth(param1, param2);
}
控制器建议
@ExceptionHandler(value = ConstraintViolationException.class)
public ErrorResponse handleInternalServerErrors(HttpServletRequest request, Exception ex) {
String message = "";
if(ex instanceof ConstraintViolationException) {
Map<String, Collection<String>> errors = new LinkedHashMap<>();
final String[] queryParam = {""};
((ConstraintViolationException) ex).getConstraintViolations().forEach(constraintViolation -> {
String queryParamPath = constraintViolation.getPropertyPath().toString();
queryParam[0] = queryParamPath.contains(".") ?
queryParamPath.substring(queryParamPath.indexOf(".") + 1) :
queryParamPath;
});
message = "The " + queryParam[0] + " is wrong";
}
return new ErrorResponse(
HttpStatus.INTERNAL_SERVER_ERROR.value(),
ex.getClass().getSimpleName(),
message);
}
测试
@Test
void TryToGetException() throws Exception {
given(service.meth(1231,1231)).willReturn(new CustObj());
MockHttpServletResponse response = mvc.perform(get("/endpoint")
.param("param1", "1231")
.param("param2", "1231")
).andReturn().getResponse();
assertEquals(response.getStatus(), HttpStatus.INTERNAL_SERVER_ERROR.value());
}
预期:200
实际:500
我应该更改什么才能在测试中获得异常?
提前致谢!
private MockMvc mvcContext;
@Autowired
private WebApplicationContext context;
@BeforeEach
public void setUp() {
mvcContext = MockMvcBuilders
.webAppContextSetup(context)
.build();
}
通过使用webAppContextSetup
,问题解决了。
它考虑了@Min、@Max(在我的例子中,通常是@Valid 注释)
所以我可以有例外来测试它!
@Test
void testConstraintViolationException() throws Exception {
MockHttpServletResponse response = mvcContext.perform(get("/endpoint")
.param("param1", "1231")
.param("param2", "1231")
assertEquals(response.getStatus(), HttpStatus.INTERNAL_SERVER_ERROR.value());
}
我尝试测试控制器的方法并获得正确的异常。它仅针对 ConstraintViolationException (javax.validation) 失败,而其他测试如 MissingServletRequestParameterException 或 MethodArgumentTypeMismatchException 测试按预期工作。
下面是我的代码的一部分,在控制器 (@Validated) 中,在我添加这些注释的方法中。 还有 ControllerAdvice 和测试。 邮递员,当我输入错误的参数 returns 时,正确的响应是: {"value":500,"name":"ConstraintViolationException","message":"param1 错误"},但由于某些奇怪的原因,JUnit 要求 200 而不是 500。
控制器
@ResponseBody
public Board method(@Min(value = 0) @Max(value = 5) @RequestParam(value = "param1", required = true) int param1,
@Min(value = 0) @Max(value = 1) @RequestParam(value = "param2", required = true) int param3){
return service.meth(param1, param2);
}
控制器建议
@ExceptionHandler(value = ConstraintViolationException.class)
public ErrorResponse handleInternalServerErrors(HttpServletRequest request, Exception ex) {
String message = "";
if(ex instanceof ConstraintViolationException) {
Map<String, Collection<String>> errors = new LinkedHashMap<>();
final String[] queryParam = {""};
((ConstraintViolationException) ex).getConstraintViolations().forEach(constraintViolation -> {
String queryParamPath = constraintViolation.getPropertyPath().toString();
queryParam[0] = queryParamPath.contains(".") ?
queryParamPath.substring(queryParamPath.indexOf(".") + 1) :
queryParamPath;
});
message = "The " + queryParam[0] + " is wrong";
}
return new ErrorResponse(
HttpStatus.INTERNAL_SERVER_ERROR.value(),
ex.getClass().getSimpleName(),
message);
}
测试
@Test
void TryToGetException() throws Exception {
given(service.meth(1231,1231)).willReturn(new CustObj());
MockHttpServletResponse response = mvc.perform(get("/endpoint")
.param("param1", "1231")
.param("param2", "1231")
).andReturn().getResponse();
assertEquals(response.getStatus(), HttpStatus.INTERNAL_SERVER_ERROR.value());
}
预期:200 实际:500
我应该更改什么才能在测试中获得异常?
提前致谢!
private MockMvc mvcContext;
@Autowired
private WebApplicationContext context;
@BeforeEach
public void setUp() {
mvcContext = MockMvcBuilders
.webAppContextSetup(context)
.build();
}
通过使用webAppContextSetup
,问题解决了。
它考虑了@Min、@Max(在我的例子中,通常是@Valid 注释)
所以我可以有例外来测试它!
@Test
void testConstraintViolationException() throws Exception {
MockHttpServletResponse response = mvcContext.perform(get("/endpoint")
.param("param1", "1231")
.param("param2", "1231")
assertEquals(response.getStatus(), HttpStatus.INTERNAL_SERVER_ERROR.value());
}