简体   繁体   English

Mockito 返回空可选

[英]Mockito returning empty optional

I am seeing a weird issue with my method returning an empty Optional in spite of mocking with mockito .尽管 mocking 和mockito ,我的方法返回一个空的Optional时发现一个奇怪的问题。 What could be going wrong?可能出了什么问题?

My test:我的测试:

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

Method under test:被测方法:

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

I end up getting 500 error instead of 200 because the mock doesn't work as expected.我最终得到 500 错误而不是 200 因为模拟没有按预期工作。

Method being mocked:被嘲笑的方法:

public Optional<Info> deleteValueSet(List<Long> ids, Boolean char, 
        Boolean digit)
{ 
   // Logic to populate Info
   Info i = new Info();  
   return Optional.of(information);
}

Info is a public static inner class in the service. Info是服务中的公共 static 内部 class。

public static class Info
{
    private Long parentValueId;
    private Long id;
    private Map<Long, Long> idInfos;
 }

Sorry guys, this was on me.对不起,伙计们,这是我的事。 The issue was that the code under test was passing null for the booleans (I had incorrectly shown valid values in the code above) and Mockito.anyBoolean() doesn't accept null .问题是被测代码通过了null的布尔值(我在上面的代码中错误地显示了有效值)并且Mockito.anyBoolean()不接受null Modifying it to Mockito.anyNull() did the trick.将其修改为Mockito.anyNull()就可以了。

You should use anyLong() for the first parameter, not anyList().您应该对第一个参数使用 anyLong(),而不是 anyList()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM