如何解决不需要的Stubbingexception

我的代码如下,

@RunWith(MockitoJUnitRunner.class) public class MyClass { private static final String code ="Test"; @Mock private MyClassDAO dao; @InjectMocks private MyClassService Service = new MyClassServiceImpl(); @Test public void testDoSearch() throws Exception { final String METHOD_NAME = logger.getName().concat(".testDoSearchEcRcfInspections()"); CriteriaDTO dto = new CriteriaDTO(); dto.setCode(code); inspectionService.searchEcRcfInspections(dto); List summaryList = new ArrayList(); inspectionsSummaryList.add(dto); when(dao.doSearch(dto)).thenReturn(inspectionsSummaryList);//got error in this line verify(dao).doSearchInspections(dto); } } 

我正在低于例外

 org.mockito.exceptions.misusing.UnnecessaryStubbingException: Unnecessary stubbings detected in test class: Test Clean & maintainable test code requires zero unnecessary code. Following stubbings are unnecessary (click to navigate to relevant line of code): 1. -> at service.Test.testDoSearch(Test.java:72) Please remove unnecessary stubbings or use 'silent' option. More info: javadoc for UnnecessaryStubbingException class. at org.mockito.internal.exceptions.Reporter.formatUnncessaryStubbingException(Reporter.java:838) at org.mockito.internal.junit.UnnecessaryStubbingsReporter.validateUnusedStubs(UnnecessaryStubbingsReporter.java:34) at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:49) at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:103) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 

请帮我解决

@RunWith(MockitoJUnitRunner.class)替换@RunWith(MockitoJUnitRunner.Silent.class)

首先,您应该检查您的测试逻辑。 通常有3个案例。 首先,你是在嘲笑错误的方法(你做了一个拼写错误或有人更改了测试过的代码,因此不再使用模拟方法)。 其次,在调用此方法之前,您的测试失败。 第三,你的逻辑属于错误if / switch分支在代码中的某处,因此不会调用mocked方法。

如果这是第一种情况,您总是希望更改代码中使用的方法的模拟方法。 它取决于第二和第三。 通常你应该删除这个模拟,如果它没用。 但有时在参数化测试中存在某些情况,这些情况应该采用不同的路径或更早地失败。 然后你可以将这个测试分成两个或多个单独的测试,但这并不总是很好看。 3个可能有3个参数提供程序的测试方法可以让你看起来不可读。 在JUnit 4的情况下,您可以使用其中任何一个来声明此exception

 @RunWith(MockitoJUnitRunner.Silent.class) 

注释或者您正在使用规则方法

 @Rule public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.LENIENT); 

或(相同的行为)

 @Rule public MockitoRule rule = MockitoJUnit.rule().silent(); 

对于JUnit 5测试,您可以使用mockito-junit-jupiter包中提供的注释来声明此exception。

 @ExtendWith(MockitoExtension.class) @MockitoSettings(strictness = Strictness.LENIENT) class JUnit5MockitoTest { } 
  when(dao.doSearch(dto)).thenReturn(inspectionsSummaryList);//got error in this line verify(dao).doSearchInspections(dto); 

when这里配置你的模拟做某事。 但是,您不必在此行之后以任何方式使用此模拟(除了进行verify )。 Mockito警告你,因此when线毫无意义。 也许你犯了一个逻辑错误?

查看堆栈跟踪的一部分,看起来您正在其他地方将dao.doSearch()存根。 更像是重复创建相同方法的存根。

 Following stubbings are unnecessary (click to navigate to relevant line of code): 1. -> at service.Test.testDoSearch(Test.java:72) Please remove unnecessary stubbings or use 'silent' option. More info: javadoc for UnnecessaryStubbingException class. 

考虑下面的测试类,例如:

 @RunWith(MockitoJUnitRunner.class) public class SomeTest { @Mock Service1 svc1Mock1; @Mock Service2 svc2Mock2; @InjectMock TestClass class; //Assume you have many dependencies and you want to set up all the stubs //in one place assuming that all your tests need these stubs. //I know that any initialization code for the test can/should be in a //@Before method. Lets assume there is another method just to create //your stubs. public void setUpRequiredStubs() { when(svc1Mock1.someMethod(any(), any())).thenReturn(something)); when(svc2Mock2.someOtherMethod(any())).thenReturn(somethingElse); } @Test public void methodUnderTest_StateUnderTest_ExpectedBehavior() { // You forget that you defined the stub for svcMock1.someMethod or //thought you could redefine it. Well you cannot. That's going to be //a problem and would throw your UnnecessaryStubbingException. when(svc1Mock1.someMethod(any(),any())).thenReturn(anyThing);//ERROR! setUpRequiredStubs(); } } 

我宁愿考虑在必要时将测试重构为存根。

如果你使用这种风格:

 @Rule public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS); 

替换为:

 @Rule public MockitoRule rule = MockitoJUnit.rule().silent();