How to mock BindingResult in Spring Boot Test

后端 未结 1 1347
忘掉有多难
忘掉有多难 2021-01-23 11:25

I have the following controller:

@RestController
@RequestMapping(value = ROOT_MAPPING)
public class GatewayController {

         


        
相关标签:
1条回答
  • 2021-01-23 11:48

    BindingResult is not a bean in the ApplicationContext. Thus, you cannot mock it via @MockBean.

    A BindingResult is created for you by Spring MVC for each incoming HTTP request.

    Thus, you don't want to mock the BindingResult. In fact, you probably don't want to mock the behavior of your RequestValidator either. Rather, you should ideally use the real implementation of your RequestValidator, pass in invalid request data (via MockMvc), and then verify the response accordingly.

    Note that you should be able to include the real implementation of your RequestValidator via @Import(RequestValidator.class) on the test class.

    0 讨论(0)
提交回复
热议问题