本文介绍了@RequestParam javax验证junit REST控制器测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以对@RequestParams上的javax验证进行弹簧控制器单元测试.

Is there a way to do spring controller unit testing for javax validations on @RequestParams.

我在控制器中有一个get方法,该方法使用@Size验证请求参数的大小.

I have a get method in controller which validates the size of the request param with @Size.

@RequestMapping(value = "/getData", method = RequestMethod.GET)
    public ResultgetData(

            @Size(min=2, max=3)
             @RequestParam String number)

有没有一种方法可以模拟junit测试大小验证器?我想验证大小为< 2或> 3时返回的错误.

Is there a way to mock junit test the size validator? I would like to validate a error returned when the size is <2 or > 3.

样本测试:

@RunWith(MockitoJUnitRunner.class)公共类MyControllerTest {

@RunWith(MockitoJUnitRunner.class)public 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,但似乎还是失败了.

I tried spring runner too ,but still seems to fail.

谢谢

推荐答案

如果我正确理解了您的问题,则可以使用 @RunWith SpringRunner @WebMvcTest 与您的控制器和您的异常处理程序类.

If I understand your issue correctly, you could use @RunWith with SpringRunner and @WebMvcTest with your controller and your exception handler classes.

由于您的问题并未显示您的控制器的外观,因此请考虑以下针对给定名称返回问候语的控制器:

As your question doesn't show what your controller looks like, let's consider the following controller that returns a greeting for the given name:

@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的异常处理程序 ,当某些验证失败时将抛出该消息:

Now let's consider an exception handler for ConstraintViolationException, which will be thrown when some validation fails:

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

这样,您可以编写测试和期望,如下所示:

With this, you could write the tests and expectations as shown below:

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

这篇关于@RequestParam javax验证junit REST控制器测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 01:25