你需要添加 matchRepo.findById(...) expection,否则mock将返回null,并且您的测试在 if (matchObj.isPresent()) 是的。
matchRepo.findById(...)
if (matchObj.isPresent())
请尝试以下代码:
when(matchRepo.findById(match.getId())).thenReturn(Optional.empty()); when(matchRepo.save(match)).thenReturn(match);