使用JUnit + Mockito测试带有if/else语句的void方法

jyztefdp  于 2022-11-08  发布在  其他
关注(0)|答案(1)|浏览(568)

使用JUinit 5和Mockito测试这种方法的正确方法是什么?我找到了两个测试用例,一个是artMerk数组大小为零,另一个是大于零,但在第二个用例中,我无法验证是否调用了方法artMerkRepository.save(toSave)和artMerkRepository.deleteInBatch(oldList)。

@Service 
public class ItemService{

   @Autowired
   ArtMerkRepository artMerkRepository;

   @Autowired 
   ItemServiceRestClient itemServiceRestClient;

   public void updateOrInsertArtMerk(String artNr) {
    LOGGER.info("Start saving ArtMerk.");
    ArtMerk[] artMerk = itemServiceRestClient.getArtMerk(artNr);
    if(artMerk != null) {
        if(artMerk.length == 0) {
            artMerkRepository.deleteByArtNr(artNr);
        }else {
            for (ArtMerk a : artMerk) {
                List<ArtMerk> aExists = artMerkRepository.findByArtNr(a.getArtNr());
                ArtMerk toSave = (aExists != null && !aExists.isEmpty()) ? UpdateUtils.updateArtMerk(aExists.get(0), a) : a;
                artMerkRepository.save(toSave);
            }
            List<ArtMerk> newList = new ArrayList<>(Arrays.asList(artMerk.clone()));
            List<ArtMerk> oldList = new ArrayList<>(artMerkRepository.findByArtNr(artNr));
            oldList.removeAll(newList);
            if (oldList != null && oldList.size() > 0) {
                artMerkRepository.deleteInBatch(oldList);
            }
        }
    }
    LOGGER.info("Successful update of ArtMerk.");
}

}
我的测试方法如下所示:

@Test
void updateOrInsertArtMerk_whenArtMerkArrSizeIsNotZero() {
    //given
    String artNr = "0001";
    ArtMerk artMerk1 = new ArtMerk();
    artMerk1.setArtNr(artNr);
    ArtMerk artMerk2 = new ArtMerk();
    artMerk2.setArtNr(artNr);
    List<ArtMerk> oldList = Arrays.asList(artMerk1, artMerk2);
    ArtMerk[] artMerks = new ArtMerk[]{artMerk1};

    //when
    when(itemServiceRestClient.getArtMerk(artNr)).thenReturn(artMerks);
    when(artMerkRepository.findByArtNr(artNr)).thenReturn(Arrays.asList(artMerks));
    when(artMerkRepository.save(artMerk1)).thenReturn(artMerk1);
    when(artMerkRepository.findByArtNr(artNr)).thenReturn(oldList);
    doNothing().when(artMerkRepository).deleteInBatch(oldList);

    //then
    itemService.updateOrInsertArtMerk(artNr);
    verify(artMerkRepository, times(2)).findByArtNr(artNr);
    verify(artMerkRepository, times(1)).save(artMerk1);
    verify(artMerkRepository,  times(1)).deleteInBatch(oldList);
}

@Test
void updateOrInsertArtMerk_whenArtMerkArrSizeIsZero() {
    //given
    String artNr = "0001";
    ArtMerk[] artMerks = new ArtMerk[]{};

    //when
    when(itemServiceRestClient.getArtMerk(artNr)).thenReturn(artMerks);
    doNothing().when(artMerkRepository).deleteByArtNr(artNr);

    //then
    itemService.updateOrInsertArtMerk(artNr);
    verify(artMerkRepository, times(1)).deleteByArtNr(artNr);
}

第一个测试方法因为UnnecessaryStubbingException而失败。

slwdgvem

slwdgvem1#

而不是

@Autowired

依赖项,则应通过构造函数注入它们。
这样,您就可以在测试期间注入一个mock对象,然后使用类似

verify(<mocked object>, times(1)).<mocked method>(argumentCaptor.capture());

相关问题