2017-03-22 17 views
11

マイコードは、以下のようにはどのように不必要なスタブ例外を解決する

@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<SearchCriteriaDTO> summaryList = new ArrayList<SearchCriteriaDTO>(); 
     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) 

を解決する方法を私を助けてくださいです

答えて

4
when(dao.doSearch(dto)).thenReturn(inspectionsSummaryList);//got error in this line 
verify(dao).doSearchInspections(dto); 

ここではモックに何かをさせるように設定します。しかし、あなたは、この行の後にもう何らかの形でこのモックを使用しないでください(verifyとは別に)。 Mockitoは、when行は無意味だと警告しています。おそらくあなたは論理エラーを起こしましたか?

+0

を私は両方と親切に文を検証する必要があるあなたの助け – VHS

+0

のおかげで、さらに – VHS

+2

コールを移動する方法を提案しますテストクラス( 'Service')上で関数が正しく反応するかどうかを調べる関数。あなたはまったくそれをしなかったので、ここで何をテストしていますか? – john16384

18

@RunWith(MockitoJUnitRunner.class)@RunWith(MockitoJUnitRunner.Silent.class)に置き換えます。

+7

ようこそ。そのようなコードをOPに置き換えて説明するには、答えを更新する価値があります。これは彼らと将来の訪問者が理解するのを助けるでしょう。 – Bugs

+5

Btw、それは '@RunWith(MockitoJUnitRunner.Silent.class)'で、* SILENTではありません。 – fgysin

+0

Kotlinでは '@RunWith(MockitoJUnitRunner.Silent :: class)' – Juancho

2

スタックトレースの一部を見ると、別の場所で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(); 
    } 
} 

私はむしろ、必要に応じスタブにし、あなたのテストをリファクタリング考えるでしょう。

0

あなたの代わりにこのスタイルを使用している場合:

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

はに置き換えます

@Rule 
public MockitoRule rule = MockitoJUnit.rule().silent(); 
関連する問題