如何在Spring Boot Test中模拟BindingResult [英] How to mock BindingResult in Spring Boot Test
问题描述
我有以下控制器:
@RestController
@RequestMapping(value = ROOT_MAPPING)
public class GatewayController
{
@Autowired
private RequestValidator requestValidator;
@InitBinder
protected void initBinder(WebDataBinder binder)
{
binder.addValidators(requestValidator);
}
@PostMapping(value = REDIRECT_MAPPING)
public ResponseEntity<ResponseDTO> redirectEndpoint(@Validated @RequestBody RequestDTO requestDTO, BindingResult result)
{
if (result.hasErrors())
{
// Handle validation errors
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
// Do other stuff
return ResponseEntity.status(HttpStatus.OK).build();
}
}
这个测试课:
@RunWith(SpringRunner.class)
@WebMvcTest(GatewayController.class)
public class GatewayControllerTest
{
@Autowired
private MockMvc mockMvc;
@MockBean
private RequestValidator requestValidator;
@MockBean
private BindingResult bindingResult;
private JacksonTester<RequestDTO> requestJacksonTester;
@Before
public void setUp() throws Exception
{
JacksonTester.initFields(this, new ObjectMapper());
Mockito.when(requestValidator.supports(ArgumentMatchers.any())).thenReturn(true);
}
@Test
public void whenRedirectWithValidationErrorsThenBadRequestReturned() throws Exception
{
RequestDTO request = new RequestDTO();
// Set some values
Mockito.when(bindingResult.hasErrors()).thenReturn(true);
mockMvc.perform(MockMvcRequestBuilders.post(ROOT_MAPPING + REDIRECT_MAPPING).contentType(MediaType.APPLICATION_JSON).content(requestJacksonTester.write(request).getJson())).andExpect(MockMvcResultMatchers.status().isBadRequest());
}
}
当我运行这段代码时,测试用例由于以下原因而失败:状态 预期:400 实际:200
When I run this code the test case fail with this reason: Status Expected :400 Actual :200
因此,我想做的是模拟BindingResult
,它作为参数传递给Controller中的redirectEndpoint
方法,以便在调用bindingResult.hasErrors()
时应返回true并通过测试用例.
So, what I want to do is to mock the BindingResult
which passed as a parameter to the redirectEndpoint
method in the Controller so that when calling bindingResult.hasErrors()
this should return true and the test case pass.
我进行了很多搜索,但是没有运气.有什么建议可以做到吗?
I did many search but with no luck. Any suggestions how to do that?
谢谢.
推荐答案
BindingResult
不是ApplicationContext
中的 bean .因此,您不能通过@MockBean
模拟 .
BindingResult
is not a bean in the ApplicationContext
. Thus, you cannot mock it via @MockBean
.
Spring MVC为每个传入的HTTP请求为您创建一个BindingResult
.
A BindingResult
is created for you by Spring MVC for each incoming HTTP request.
因此,您不想模拟BindingResult
.实际上,您可能也不想模拟RequestValidator
的行为.相反,理想情况下,您应该使用RequestValidator
的真实实现,通过MockMvc
传入无效请求数据,然后相应地验证响应.
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.
请注意,您应该能够通过测试类上的@Import(RequestValidator.class)
include RequestValidator
的真正实现.
Note that you should be able to include the real implementation of your RequestValidator
via @Import(RequestValidator.class)
on the test class.
这篇关于如何在Spring Boot Test中模拟BindingResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!