2017-03-22 42 views
11

mojego kodu jest jak poniżej,Jak rozwiązać wyjątek zbędnych stubbing

@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); 

     } 
} 

jestem coraz niżej wyjątkiem

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) 

Proszę mi pomóc jak rozwiązać

Odpowiedz

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

The when tutaj konfiguruje twoją próbę, aby coś zrobić. Jednak już po tej linii nie używasz tej metody w żaden sposób (poza robieniem verify). Mockito ostrzega, że ​​linia when jest bezcelowa. Być może popełniłeś błąd logiczny?

+0

dzięki za pomoc – VHS

+0

muszę zarówno kiedy i weryfikacji sprawozdań uprzejmie sugerują jak przenieść dalsze – VHS

+2

połączenia funkcja na twojej klasie testowej ('Service'), aby sprawdzić, czy działa poprawnie. Wcale tego nie zrobiłeś, więc co tu testujesz? – john16384

18

Wymień @RunWith(MockitoJUnitRunner.class) na @RunWith(MockitoJUnitRunner.Silent.class).

+7

Witamy. Byłoby warto zaktualizować swoją odpowiedź, aby wyjaśnić, dlaczego OP powinien zastąpić taki kod. To pomoże im i przyszłym odwiedzającym zrozumieć. – Bugs

+5

Btw, jest to '@RunWith (MockitoJUnitRunner.Silent.class)' i * nie * SILENT – fgysin

+0

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

2

Patrząc na część śladu stosu, wygląda na to, że w innym miejscu dochodzisz do kikuta dao.doSearch(). Bardziej lubię powtarzanie tworzenia odcinków tej samej metody.

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. 

Rozważmy poniżej Klasa testu na przykład:

@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(); 
    } 
} 

Wolałbym rozważa refactoring swoje testy na skrótową, gdzie jest to konieczne.

0

Jeśli używasz tego stylu zamiast:

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

zastąpić go:

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