有没有一种方法可以对@RequestParams上的javax验证进行弹簧控制器单元测试。
我在控制器中有一个get方法,该方法使用@Size验证请求参数的大小。
@RequestMapping(value = "/getData", method = RequestMethod.GET)
public ResultgetData(
@Size(min=2, max=3)
@RequestParam String number)
有没有一种方法可以模拟junit测试大小验证器?我想验证大小为<2或> 3时返回的错误。
样本测试:
@RunWith(MockitoJUnitRunner.class) 公共类MyControllerTest {
private MockMvc mockMvc;
@InjectMocks
private MyController myControllerMock;
@Before
public void initTest() {
mockMvc = MockMvcBuilders.standaloneSetup(customerInsuranceControllerMock).setControllerAdvice(exceptionHandler).build();
}
@Test
public void getEmptyData() throws Exception{
mockMvc.perform(MockMvcRequestBuilders.get(
"/getData?number={number}"
, "")
.andExpect(MockMvcResultMatchers.status().isBadRequest()); // This is failing. It returns a success as javax @Size is not triggered.When a empty string is passed , it should be bad request
}
我也尝试过SpringRunner,但似乎还是失败了。
谢谢
答案 0 :(得分:0)
如果我正确理解了您的问题,则可以将@RunWith
与SpringRunner
和@WebMvcTest
一起用于控制器和异常处理程序类。
由于您的问题并未显示控制器的外观,因此请考虑以下控制器,该控制器返回给定名称的问候语:
@Data
public class Greeting {
private String content;
}
@Validated
@RestController
public class GreetingController {
@GetMapping(path = "/greeting", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Greeting> getGreeting(
@RequestParam @Size(min = 2, max = 10) String name) {
Greeting greeting = new Greeting();
greeting.setContent("Hello " + name + "!");
return ResponseEntity.ok(greeting);
}
}
现在让我们考虑ConstraintViolationException
的异常处理程序,当某些验证失败时将抛出该异常处理程序:
@Data
public class ApiError {
private String message;
private HttpStatus status;
private Object details;
}
@Data
public class InvalidValue {
private String name;
private Object value;
private String message;
}
@ControllerAdvice
public class WebApiExceptionHandler {
@ExceptionHandler({ConstraintViolationException.class})
public ResponseEntity<Object> handleConstraintViolation(ConstraintViolationException ex,
WebRequest request) {
List<InvalidValue> invalidValues = ex.getConstraintViolations()
.stream()
.map(this::toInvalidValue)
.collect(toList());
ApiError apiError = new ApiError();
apiError.setMessage("Validation error");
apiError.setStatus(HttpStatus.BAD_REQUEST);
apiError.setDetails(invalidValues);
return new ResponseEntity<>(apiError, new HttpHeaders(), apiError.getStatus());
}
private InvalidValue toInvalidValue(ConstraintViolation violation) {
InvalidValue invalidValue = new InvalidValue();
invalidValue.setName(violation.getPropertyPath().toString());
invalidValue.setValue(violation.getInvalidValue());
invalidValue.setMessage(violation.getMessage());
return invalidValue;
}
}
这样,您可以编写测试和期望,如下所示:
@RunWith(SpringRunner.class)
@WebMvcTest({GreetingController.class, WebApiExceptionHandler.class})
public class GreetingControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
@SneakyThrows
public void getGreeting_shouldReturn200_whenNameIsValid() {
mockMvc.perform(
get("/greeting")
.param("name", "foo")
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.*", hasSize(1)))
.andExpect(jsonPath("$.content").value("Hello foo!"));
}
@Test
@SneakyThrows
public void getGreeting_shouldReturn400_whenNameIsInvalid() {
mockMvc.perform(get("/greeting").param("name", "_"))
.andDo(print())
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.*", hasSize(3)))
.andExpect(jsonPath("$.message").value("Validation error"))
.andExpect(jsonPath("$.status").value("BAD_REQUEST"))
.andExpect(jsonPath("$.details", hasSize(1)))
.andExpect(jsonPath("$.details[0].*", hasSize(3)))
.andExpect(jsonPath("$.details[0].name", is("getGreeting.name")))
.andExpect(jsonPath("$.details[0].value", is("_")))
.andExpect(jsonPath("$.details[0].message", is("size must be between 2 and 10")));
}
}
答案 1 :(得分:0)
您有2个问题,
1.您需要使用@Length
来验证字符串的长度,@Size
是要验证集合中的项数,例如List
。如果您说它必须是2或3的整数,则需要@Min(2) @Max(3)
。
2.您的控制器必须带有@Validated
注释。