Class ToBeTested{
@Autowired
private Repo1Src repo1;// JPA repository
@Autowired
private Repo2Src repo2;// JPA repository
public Map<String, Object> getAll( long id) {
List<Repo1Res> repo1Res= repo1.findAll(id);
List<String> systems = new ArrayList<>(repo1Res.size());
Map<String, SysSceDto> resultMap= getCorrespondanceSystemSytemDot(repo1Res);
systems.addAll(resultMap.keySet());
List<RepoRes2> repo2Res= repo2.findAll(systems,
id,
1L);
return new HashMap();
}
}
@RunWith(MockitoJUnitRunner.class)
class ToBeTestedTests{
@Mock
private Repo1Src repo1;
@Mock
private Repo2Src repo2;
@InjectMocks
private ToBeTested toBeTested;
@Test
public void test(){
List<Repo1Res> lst1= new ArrayList<>();
List<Repo2Res> lst2= new ArrayList<>();
List<String> systems = new ArrayList<>();
generate(lst1, lst2, systems );
when(repo1.findAll(1)).thenReturn(lst1);
when(repo2.findAll(systems , 1, 1L)).thenReturn(lst2);
toBeTested.getAll(1);
}
}
我有以下异常:org.mockito.exceptions.misusing.unnecessarystubbingexception当我试图运行测试时,它是我调用when(repo2.findall(systems,1,1l))的地方;
堆栈跟踪:
org.mockito.exceptions.misusing.UnnecessaryStubbingException:
Unnecessary stubbings detected in test class: ToBeTestedTests
Clean & maintainable test code requires zero unnecessary code.
Following stubbings are unnecessary (click to navigate to relevant line of code):
1. -> at ToBeTestedTests.test(ToBeTestedTests.java:22)
Please remove unnecessary stubbings or use 'lenient' strictness. More info: javadoc for UnnecessaryStubbingException class.
1条答案
按热度按时间gxwragnw1#
您的测试失败,因为第二个存储库的存根
repo2
测试期间未调用。看来,存根的设置
不完全反映你的观点
repo2.findAll()
在测试执行期间调用。我猜是因为systems
变量。作为第一个修复,您可以打开存根并接受任何列表:
... 然后调试或打印
systems
变量来理解差异在哪里。然而,这个失败或多或少也是mockito发出的警告,因为您正在存根未使用的方法。在mockito的最新版本中,这变得更加严格,并且每当您存根测试期间未使用的内容时,测试就会失败。
可以覆盖默认值
STRICTNESS
或者LENIENT
(又名。我不在乎)或者WARN
至少有警告输出,但没有失败的测试: