Mockito 返回空可选
Mockito returning empty optional
尽管使用 mockito
进行模拟,但我的方法返回空 Optional
时出现了一个奇怪的问题。可能出了什么问题?
我的测试:
@RunWith(SpringRunner.class)
@WebMvcTest(InfoController.class)
@ActiveProfiles("test")
public class InfoControllerTest
{
@Autowired
private MockMvc mockMvc;
@MockBean
private Service service;
@Autowired
private ObjectMapper objectMapper;
@Test
public void testDeleteValueSet() throws JsonProcessingException, Exception
{
DeleteValueSetContainer c1 = new DeleteValueSetContainer();
c1.setValueSetIds(Collections.singletonList(1L));
Mockito.when(service.deleteValueSet(Mockito.anyList(),
Mockito.anyBoolean(), Mockito.anyBoolean())).thenReturn(Optional.of(new Info()));
mockMvc.perform(delete("/deleteValueSet").
accept(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(c1))
.contentType(MediaType.APPLICATION_JSON)).andExpect(status().isNoContent());
}
}
正在测试的方法:
@DeleteMapping("/deleteValueSet")
public ResponseEntity<Long> deleteValueSet(@RequestBody(required=true) DeleteValueSetContainer
deleteValueSetContainer)
{
Optional<Info> optional = service.deleteValueSet(valueSetIds,
null, null);
if(optional.isPresent())
{
Info valueInformation = optional.get();
Long parentValueId = valueInformation.getParentValueId();
if(parentValueId != null && parentValueId != 0L)
{
return ResponseEntity.created(ServletUriComponentsBuilder.
fromCurrentRequest().build().toUri())
.body((Long)valueInformation.getParentValueId());
}
return ResponseEntity.noContent().build();
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
我最终得到 500 个错误而不是 200 个错误,因为模拟没有按预期工作。
被模拟的方法:
public Optional<Info> deleteValueSet(List<Long> ids, Boolean char,
Boolean digit)
{
// Logic to populate Info
Info i = new Info();
return Optional.of(information);
}
Info
是服务中的 public 静态内部 class。
public static class Info
{
private Long parentValueId;
private Long id;
private Map<Long, Long> idInfos;
}
您应该使用 anyLong() 作为第一个参数,而不是 anyList()。
对不起伙计们,这是我的责任。问题是被测代码为布尔值传递了 null
(我在上面的代码中错误地显示了有效值)并且 Mockito.anyBoolean()
不接受 null
。将其修改为 Mockito.anyNull()
就可以了。
尽管使用 mockito
进行模拟,但我的方法返回空 Optional
时出现了一个奇怪的问题。可能出了什么问题?
我的测试:
@RunWith(SpringRunner.class)
@WebMvcTest(InfoController.class)
@ActiveProfiles("test")
public class InfoControllerTest
{
@Autowired
private MockMvc mockMvc;
@MockBean
private Service service;
@Autowired
private ObjectMapper objectMapper;
@Test
public void testDeleteValueSet() throws JsonProcessingException, Exception
{
DeleteValueSetContainer c1 = new DeleteValueSetContainer();
c1.setValueSetIds(Collections.singletonList(1L));
Mockito.when(service.deleteValueSet(Mockito.anyList(),
Mockito.anyBoolean(), Mockito.anyBoolean())).thenReturn(Optional.of(new Info()));
mockMvc.perform(delete("/deleteValueSet").
accept(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(c1))
.contentType(MediaType.APPLICATION_JSON)).andExpect(status().isNoContent());
}
}
正在测试的方法:
@DeleteMapping("/deleteValueSet")
public ResponseEntity<Long> deleteValueSet(@RequestBody(required=true) DeleteValueSetContainer
deleteValueSetContainer)
{
Optional<Info> optional = service.deleteValueSet(valueSetIds,
null, null);
if(optional.isPresent())
{
Info valueInformation = optional.get();
Long parentValueId = valueInformation.getParentValueId();
if(parentValueId != null && parentValueId != 0L)
{
return ResponseEntity.created(ServletUriComponentsBuilder.
fromCurrentRequest().build().toUri())
.body((Long)valueInformation.getParentValueId());
}
return ResponseEntity.noContent().build();
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
我最终得到 500 个错误而不是 200 个错误,因为模拟没有按预期工作。
被模拟的方法:
public Optional<Info> deleteValueSet(List<Long> ids, Boolean char,
Boolean digit)
{
// Logic to populate Info
Info i = new Info();
return Optional.of(information);
}
Info
是服务中的 public 静态内部 class。
public static class Info
{
private Long parentValueId;
private Long id;
private Map<Long, Long> idInfos;
}
您应该使用 anyLong() 作为第一个参数,而不是 anyList()。
对不起伙计们,这是我的责任。问题是被测代码为布尔值传递了 null
(我在上面的代码中错误地显示了有效值)并且 Mockito.anyBoolean()
不接受 null
。将其修改为 Mockito.anyNull()
就可以了。