Spring MVC测试-将null值作为参数传递



我有一个控制器,它有一个方法可以接受null值作为参数,但当我尝试测试时遇到了一个错误:java.lang.IllegalArgumentException:'values'不能为空

@GetMapping("/search")
public ResponseEntity<List<ProductionCycleExecutionDTO>> search(@RequestParam(required = false) String countryId,
@RequestParam(required = false) String yearMonth, @RequestParam(required = false) String status,
@RequestParam(required = false) String name) {
log.info("search");
...

测试等级:

@Autowired
private MockMvc mvc;
@MockBean
private ProductionCycleExecutionService prodCycleExecService;
...
@Test
public void givenProdCycle_whenSearchByParams_thenReturnJsonOk() throws Exception {
log.debug("Test givenProdCycle_whenSearchByParams_thenReturnJsonOk()");
List<ProductionCycleExecutionDTO> listProdCycleExec = new ArrayList<>();
ProductionCycleExecutionDTO productionCycleExecutionDTO = ProductionCycleExecutionDTO.builder().cycleId("1")
.yearMonth("202005").name("Start_Cycle_TransferFile").status("AC").build();
listProdCycleExec.add(productionCycleExecutionDTO);
when(prodCycleExecService.searchByParams(null, null, null, null)).thenReturn(listProdCycleExec);
mvc.perform(get("/production-cycle-execution/search").param("countryId", null).param("yearMonth", null)
.param("status", null).param("name", null)).andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$", hasSize(1)));
...

如何测试将null传递给get方法?

只向get方法不传递任何信息。

mvc.perform(get("/production-cycle-execution/search")).andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$", hasSize(1)));

最新更新