类org.mockito.exceptions.verification.NoInteractionsWanted源码实例Demo

下面列出了怎么用org.mockito.exceptions.verification.NoInteractionsWanted的API类实例代码及写法,或者点击链接到github查看源代码。

@Test
public void shouldVerifyOneMockButFailOnOther() throws Exception {
    List list = mock(List.class);
    Map map = mock(Map.class);

    list.add("one");
    list.add("one");
    
    map.put("one", 1);
    
    verify(list, times(2)).add("one");
    
    verifyNoMoreInteractions(list);
    try {
        verifyZeroInteractions(map);
        fail();
    } catch (NoInteractionsWanted e) {}
}
 
@Test
public void shouldAllowToExcludeStubsForVerification() throws Exception {
    //given
    when(mock.simpleMethod()).thenReturn("foo");

    //when
    String stubbed = mock.simpleMethod(); //irrelevant call because it is stubbing
    mock.objectArgMethod(stubbed);

    //then
    verify(mock).objectArgMethod("foo");

    //verifyNoMoreInteractions fails:
    try { verifyNoMoreInteractions(mock); fail(); } catch (NoInteractionsWanted e) {};
    
    //but it works when stubs are ignored:
    ignoreStubs(mock);
    verifyNoMoreInteractions(mock);
}
 
@Test
public void shouldPrintFirstUnexpectedInvocation() {
    mock.oneArg(true);
    mock.oneArg(false);
    mock.threeArgumentMethod(1, "2", "3");

    verify(mock).oneArg(true);
    try {
        verifyNoMoreInteractions(mock);
        fail();
    } catch (NoInteractionsWanted e) {
        String expectedMessage =
                "\n" +
                "No interactions wanted here:" +
                "\n" +
                "-> at";
        assertContains(expectedMessage, e.getMessage());

        String expectedCause =
                "\n" +
                "But found this interaction:" +
                "\n" +
                "-> at";
        assertContains(expectedCause, e.getMessage());
    }
}
 
@Test
public void shouldPrintFirstUnexpectedInvocationWhenVerifyingZeroInteractions() {
    mock.twoArgumentMethod(1, 2);
    mock.threeArgumentMethod(1, "2", "3");

    try {
        verifyZeroInteractions(mock);
        fail();
    } catch (NoInteractionsWanted e) {
        String expected =
                "\n" +
                "No interactions wanted here:" +
                "\n" +
                "-> at";

        assertContains(expected, e.getMessage());

        String expectedCause =
            "\n" +
            "But found this interaction:" +
            "\n" +
            "-> at";

        assertContains(expectedCause, e.getMessage());
    }
}
 
@Test
public void shouldVerifyOneMockButFailOnOther() throws Exception {
    List list = mock(List.class);
    Map map = mock(Map.class);

    list.add("one");
    list.add("one");
    
    map.put("one", 1);
    
    verify(list, times(2)).add("one");
    
    verifyNoMoreInteractions(list);
    try {
        verifyZeroInteractions(map);
        fail();
    } catch (NoInteractionsWanted e) {}
}
 
源代码6 项目: astor   文件: VerificationExcludingStubsTest.java
@Test
public void shouldAllowToExcludeStubsForVerification() throws Exception {
    //given
    when(mock.simpleMethod()).thenReturn("foo");

    //when
    String stubbed = mock.simpleMethod(); //irrelevant call because it is stubbing
    mock.objectArgMethod(stubbed);

    //then
    verify(mock).objectArgMethod("foo");

    //verifyNoMoreInteractions fails:
    try { verifyNoMoreInteractions(mock); fail(); } catch (NoInteractionsWanted e) {};
    
    //but it works when stubs are ignored:
    ignoreStubs(mock);
    verifyNoMoreInteractions(mock);
}
 
@Test
public void should_print_first_unexpected_invocation() {
    mock.oneArg(true);
    mock.oneArg(false);
    mock.threeArgumentMethod(1, "2", "3");

    verify(mock).oneArg(true);
    try {
        verifyNoMoreInteractions(mock);
        fail();
    } catch (NoInteractionsWanted e) {
        String expectedMessage =
                "\n" +
                "No interactions wanted here:" +
                "\n" +
                "-> at";
        assertContains(expectedMessage, e.getMessage());

        String expectedCause =
                "\n" +
                "But found this interaction:" +
                "\n" +
                "-> at";
        assertContains(expectedCause, e.getMessage());
    }
}
 
@Test
public void should_print_first_unexpected_invocation_when_verifying_zero_interactions() {
    mock.twoArgumentMethod(1, 2);
    mock.threeArgumentMethod(1, "2", "3");

    try {
        verifyZeroInteractions(mock);
        fail();
    } catch (NoInteractionsWanted e) {
        String expected =
                "\n" +
                "No interactions wanted here:" +
                "\n" +
                "-> at";

        assertContains(expected, e.getMessage());

        String expectedCause =
            "\n" +
            "But found this interaction:" +
            "\n" +
            "-> at";

        assertContains(expectedCause, e.getMessage());
    }
}
 
源代码9 项目: dexmaker   文件: GeneralMocking.java
@Test
public void verifyAdditionalInvocations() {
    TestClass t = mock(TestClass.class);

    t.returnA();
    t.returnA();

    try {
        verifyNoMoreInteractions(t);
    } catch (NoInteractionsWanted e) {
        try {
            throw new Exception();
        } catch (Exception here) {
            // The error message should indicate where the additional invocations have been made
            assertTrue(e.getMessage(),
                    e.getMessage().contains(here.getStackTrace()[0].getMethodName()));
        }
    }
}
 
源代码10 项目: mockito-cookbook   文件: TaxTransfererTestNgTest.java
/** contains expected only for the test to pass **/
@Test(expectedExceptions = NoInteractionsWanted.class)
public void should_fail_on_verifying_no_more_interactions_due_to_existing_stubbing() {
    // given
    Person person = new Person();
    given(taxService.sendStatisticsReport()).willReturn(true);

    // when
    boolean success = systemUnderTest.transferTaxFor(person);

    // then
    verify(taxService).transferTaxFor(person);
    verifyNoMoreInteractions(taxService);
 then(success).isTrue();
}
 
源代码11 项目: mockito-cookbook   文件: TaxTransfererTest.java
/** contains expected only for the test to pass **/
@Test(expected = NoInteractionsWanted.class)
public void should_fail_on_verifying_no_more_interactions_due_to_existing_stubbing() {
    // given
    Person person = new Person();
    given(taxService.sendStatisticsReport()).willReturn(true);

    // when
    boolean success = systemUnderTest.transferTaxFor(person);

    // then
    verify(taxService).transferTaxFor(person);
    verifyNoMoreInteractions(taxService);
 then(success).isTrue();
}
 
源代码12 项目: mockito-cookbook   文件: TaxTransfererTestNgTest.java
/** contains expected only for the test to pass **/
@Test(expectedExceptions = NoInteractionsWanted.class)
public void should_fail_on_verifying_no_more_interactions_due_to_existing_stubbing() {
    // given
    Person person = new Person();
    given(taxService.sendStatisticsReport()).willReturn(true);

    // when
    boolean success = systemUnderTest.transferTaxFor(person);

    // then
    verify(taxService).transferTaxFor(person);
    verifyNoMoreInteractions(taxService);
    assertThat(success, is(true));
}
 
源代码13 项目: mockito-cookbook   文件: TaxTransfererTest.java
/** contains expected only for the test to pass **/
@Test(expected = NoInteractionsWanted.class)
public void should_fail_on_verifying_no_more_interactions_due_to_existing_stubbing() {
    // given
    Person person = new Person();
    given(taxService.sendStatisticsReport()).willReturn(true);

    // when
    boolean success = systemUnderTest.transferTaxFor(person);

    // then
    verify(taxService).transferTaxFor(person);
    verifyNoMoreInteractions(taxService);
    assertThat(success, is(true));
}
 
源代码14 项目: astor   文件: Reporter.java
public void noMoreInteractionsWanted(Invocation undesired, List<VerificationAwareInvocation> invocations) {
    ScenarioPrinter scenarioPrinter = new ScenarioPrinter();
    String scenario = scenarioPrinter.print(invocations);

    throw new NoInteractionsWanted(join(
            "No interactions wanted here:",
            new Location(),
            "But found this interaction:",
            undesired.getLocation(),
            scenario
    ));
}
 
源代码15 项目: astor   文件: BasicStubbingTest.java
@Test
public void shouldStubbingBeTreatedAsInteraction() throws Exception {
    when(mock.booleanReturningMethod()).thenReturn(true);
    
    mock.booleanReturningMethod();
    
    try {
        verifyNoMoreInteractions(mock);
        fail();
    } catch (NoInteractionsWanted e) {}
}
 
源代码16 项目: astor   文件: StubbingUsingDoReturnTest.java
@Test
public void shouldStubbingBeTreatedAsInteraction() throws Exception {
    doReturn("foo").when(mock).simpleMethod();
    mock.simpleMethod();
    try {
        verifyNoMoreInteractions(mock);
        fail();
    } catch (NoInteractionsWanted e) {}
}
 
源代码17 项目: astor   文件: DeprecatedStubbingTest.java
@Test
public void shouldStubbingBeTreatedAsInteraction() throws Exception {
    stub(mock.booleanReturningMethod()).toReturn(true);
    
    mock.booleanReturningMethod();
    
    try {
        verifyNoMoreInteractions(mock);
        fail();
    } catch (NoInteractionsWanted e) {}
}
 
源代码18 项目: astor   文件: UsingVarargsTest.java
@Test
public void shouldVerifyObjectVarargs() {
    mock.withObjectVarargs(1);
    mock.withObjectVarargs(2, "1", new ArrayList<Object>(), new Integer(1));
    mock.withObjectVarargs(3, new Integer(1));

    verify(mock).withObjectVarargs(1);
    verify(mock).withObjectVarargs(2, "1", new ArrayList<Object>(), new Integer(1));
    try {
        verifyNoMoreInteractions(mock);
        fail();
    } catch (NoInteractionsWanted e) {}
}
 
源代码19 项目: astor   文件: StackTraceFilteringTest.java
@Test
public void shouldFilterStackTraceOnVerifyNoMoreInteractions() {
    mock.oneArg(true);
    try {
        verifyNoMoreInteractions(mock);
        fail();
    } catch (NoInteractionsWanted e) {
        assertThat(e, hasFirstMethodInStackTrace("shouldFilterStackTraceOnVerifyNoMoreInteractions"));
    }
}
 
源代码20 项目: astor   文件: StackTraceFilteringTest.java
@Test
public void shouldFilterStackTraceOnVerifyZeroInteractions() {
    mock.oneArg(true);
    try {
        verifyZeroInteractions(mock);
        fail();
    } catch (NoInteractionsWanted e) {
        assertThat(e, hasFirstMethodInStackTrace("shouldFilterStackTraceOnVerifyZeroInteractions"));
    }
}
 
@Test
public void shouldFailOnNoMoreInteractions() {
    inOrder.verify(mockOne, atLeastOnce()).simpleMethod(1);
    inOrder.verify(mockThree).simpleMethod(3);
    inOrder.verify(mockThree).simpleMethod(4);
    
    try {
        verifyNoMoreInteractions(mockOne, mockTwo, mockThree);
        fail();
    } catch (NoInteractionsWanted e) {}
}
 
@Test
public void shouldFailOnNoMoreInteractionsOnMockVerifiedInOrder() {
    inOrder.verify(mockOne, atLeastOnce()).simpleMethod(1);
    inOrder.verify(mockThree).simpleMethod(3);
    verify(mockTwo).simpleMethod(2);
    
    try {
        verifyNoMoreInteractions(mockOne, mockTwo, mockThree);
        fail();
    } catch (NoInteractionsWanted e) {}
}
 
源代码23 项目: astor   文件: AtMostXVerificationTest.java
@Test
public void shouldDetectUnverifiedInMarkInteractionsAsVerified() throws Exception {
    mock.clear();
    mock.clear();
    undesiredInteraction();
    
    verify(mock, atMost(3)).clear();
    try {
        verifyNoMoreInteractions(mock);
        fail();
    } catch(NoInteractionsWanted e) {
        assertContains("undesiredInteraction(", e.getMessage());
    }
}
 
源代码24 项目: astor   文件: NoMoreInteractionsVerificationTest.java
@Test
public void shouldFailZeroInteractionsVerification() throws Exception {
    mock.clear();
    
    try {
        verifyZeroInteractions(mock);
        fail();
    } catch (NoInteractionsWanted e) {}
}
 
源代码25 项目: astor   文件: NoMoreInteractionsVerificationTest.java
@Test
public void shouldFailNoMoreInteractionsVerification() throws Exception {
    mock.clear();
    
    try {
        verifyNoMoreInteractions(mock);
        fail();
    } catch (NoInteractionsWanted e) {}
}
 
源代码26 项目: astor   文件: NoMoreInteractionsVerificationTest.java
@Test
public void shouldPrintAllInvocationsWhenVerifyingNoMoreInvocations() throws Exception {
    mock.add(1);
    mock.add(2);
    mock.clear();
    
    verify(mock).add(2);
    try {
        verifyNoMoreInteractions(mock);
        fail();
    } catch (NoInteractionsWanted e) {
        assertContains("list of all invocations", e.getMessage());
    }
}
 
源代码27 项目: astor   文件: NoMoreInteractionsVerificationTest.java
@Test
public void shouldNotContainAllInvocationsWhenSingleUnwantedFound() throws Exception {
    mock.add(1);
    
    try {
        verifyNoMoreInteractions(mock);
        fail();
    } catch (NoInteractionsWanted e) {
        assertNotContains("list of all invocations", e.getMessage());
    }
}
 
源代码28 项目: astor   文件: OnlyVerificationTest.java
@Test
public void shouldFailIfMethodWasInvokedMoreThanOnce() {
	mock.clear();
	mock.clear();
	try {
		verify(mock, only()).clear();
		fail();
	} catch (NoInteractionsWanted e) {}
}
 
源代码29 项目: astor   文件: OnlyVerificationTest.java
@Test
public void shouldFailIfExtraMethodWithDifferentArgsFound() {
    mock.get(0);
    mock.get(2);
    try {
        verify(mock, only()).get(2);
        fail();
    } catch (NoInteractionsWanted e) {}
}
 
@Test
public void shouldAllowTwoTimesOnMockTwo() {
    InOrder inOrder = inOrder(mockTwo, mockThree);

    inOrder.verify(mockTwo, times(2)).simpleMethod(2);
    try {
        verifyNoMoreInteractions(mockTwo);
        fail();
    } catch (NoInteractionsWanted e) {}
}
 
 类所在包
 同包方法