org.mockito.exceptions.verification.NoInteractionsWanted Java Examples

The following examples show how to use org.mockito.exceptions.verification.NoInteractionsWanted. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DescriptiveMessagesWhenVerificationFailsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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());
    }
}
 
Example #2
Source File: NoMoreInteractionsVerificationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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) {}
}
 
Example #3
Source File: NoMoreInteractionsExcludingStubsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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);
}
 
Example #4
Source File: NoMoreInteractionsVerificationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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) {}
}
 
Example #5
Source File: VerificationExcludingStubsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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);
}
 
Example #6
Source File: DescriptiveMessagesWhenVerificationFailsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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());
    }
}
 
Example #7
Source File: DescriptiveMessagesWhenVerificationFailsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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());
    }
}
 
Example #8
Source File: DescriptiveMessagesWhenVerificationFailsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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());
    }
}
 
Example #9
Source File: GeneralMocking.java    From dexmaker with Apache License 2.0 6 votes vote down vote up
@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()));
        }
    }
}
 
Example #10
Source File: OnlyVerificationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldFailIfExtraMethodWithDifferentArgsFound() {
    mock.get(0);
    mock.get(2);
    try {
        verify(mock, only()).get(2);
        fail();
    } catch (NoInteractionsWanted e) {}
}
 
Example #11
Source File: VerifyStatic.java    From dexmaker with Apache License 2.0 5 votes vote down vote up
@Test(expected = NoInteractionsWanted.class)
public void zeroInvocationsThrowsIfThereWasAnInvocation() throws Exception {
    MockitoSession session = mockitoSession().mockStatic(EchoClass.class).startMocking();
    try {
        EchoClass.echo("marco!");
        verifyZeroInteractions(staticMockMarker(EchoClass.class));
        fail();
    } finally {
        session.finishMocking();
    }
}
 
Example #12
Source File: ZeroInvocationsMatcher.java    From lambda with MIT License 5 votes vote down vote up
@Override
public boolean matches(Object item) {
    try {
        verifyNoMoreInteractions(item);
        return true;
    } catch (NoInteractionsWanted unexpectedInteractions) {
        return false;
    } catch (NotAMockException notVerifiable) {
        throw new AssertionError("Can't do verifications on non-mocked objects.");
    }
}
 
Example #13
Source File: MockitoVerifyExamplesIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test(expected = NoInteractionsWanted.class)
public final void givenUnverifiedInteraction_whenVerifyingNoUnexpectedInteractions_thenFail() {
    final List<String> mockedList = mock(MyList.class);
    mockedList.size();
    mockedList.clear();

    verify(mockedList).size();
    verifyNoMoreInteractions(mockedList);
}
 
Example #14
Source File: MockitoVerifyExamplesUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test(expected = NoInteractionsWanted.class)
public final void givenUnverifiedInteraction_whenVerifyingNoUnexpectedInteractions_thenFail() {
    final List<String> mockedList = mock(MyList.class);
    mockedList.size();
    mockedList.clear();

    verify(mockedList).size();
    verifyNoMoreInteractions(mockedList);
}
 
Example #15
Source File: BasicVerificationInOrderTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldFailOnVerifyNoMoreInteractions() {
    inOrder.verify(mockOne).simpleMethod(1);
    inOrder.verify(mockTwo, times(2)).simpleMethod(2);
    inOrder.verify(mockThree).simpleMethod(3);
    inOrder.verify(mockTwo).simpleMethod(2);

    try {
        verifyNoMoreInteractions(mockOne, mockTwo, mockThree);
        fail();
    } catch (NoInteractionsWanted e) {
    }
}
 
Example #16
Source File: SpyingOnRealObjectsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldVerifyNoMoreInteractionsAndFail() {
    spy.add("one");
    spy.add("two");
    
    verify(spy).add("one");
    try {
        verifyNoMoreInteractions(spy);
        fail();
    } catch (NoInteractionsWanted e) {}
}
 
Example #17
Source File: BasicVerificationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldDetectRedundantInvocation() throws Exception {
    mock.clear();
    mock.add("foo");
    mock.add("bar");

    verify(mock).clear();
    verify(mock).add("foo");

    try {
        verifyNoMoreInteractions(mock);
        fail();
    } catch (NoInteractionsWanted e) {}
}
 
Example #18
Source File: RelaxedVerificationInOrderTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldAllowFirstChunkBeforeLastInvocation() {
    inOrder.verify(mockTwo, times(2)).simpleMethod(2);
    inOrder.verify(mockOne).simpleMethod(4);
    
    try {
        verifyNoMoreInteractions(mockTwo);
        fail();
    } catch (NoInteractionsWanted e) {}
}
 
Example #19
Source File: OnlyVerificationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldFailIfMethodWasInvokedMoreThanOnce() {
	mock.clear();
	mock.clear();
	try {
		verify(mock, only()).clear();
		fail();
	} catch (NoInteractionsWanted e) {}
}
 
Example #20
Source File: NoMoreInteractionsVerificationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldNotContainAllInvocationsWhenSingleUnwantedFound() throws Exception {
    mock.add(1);
    
    try {
        verifyNoMoreInteractions(mock);
        fail();
    } catch (NoInteractionsWanted e) {
        assertNotContains("list of all invocations", e.getMessage());
    }
}
 
Example #21
Source File: NoMoreInteractionsVerificationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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());
    }
}
 
Example #22
Source File: NoMoreInteractionsVerificationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldFailNoMoreInteractionsVerification() throws Exception {
    mock.clear();
    
    try {
        verifyNoMoreInteractions(mock);
        fail();
    } catch (NoInteractionsWanted e) {}
}
 
Example #23
Source File: NoMoreInteractionsVerificationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldFailZeroInteractionsVerification() throws Exception {
    mock.clear();
    
    try {
        verifyZeroInteractions(mock);
        fail();
    } catch (NoInteractionsWanted e) {}
}
 
Example #24
Source File: AtMostXVerificationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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());
    }
}
 
Example #25
Source File: VerificationInOrderMixedWithOrdiraryVerificationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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) {}
}
 
Example #26
Source File: VerificationInOrderMixedWithOrdiraryVerificationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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) {}
}
 
Example #27
Source File: StackTraceFilteringTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldFilterStackTraceOnVerifyZeroInteractions() {
    mock.oneArg(true);
    try {
        verifyZeroInteractions(mock);
        fail();
    } catch (NoInteractionsWanted e) {
        assertThat(e, hasFirstMethodInStackTrace("shouldFilterStackTraceOnVerifyZeroInteractions"));
    }
}
 
Example #28
Source File: StackTraceFilteringTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldFilterStackTraceOnVerifyNoMoreInteractions() {
    mock.oneArg(true);
    try {
        verifyNoMoreInteractions(mock);
        fail();
    } catch (NoInteractionsWanted e) {
        assertThat(e, hasFirstMethodInStackTrace("shouldFilterStackTraceOnVerifyNoMoreInteractions"));
    }
}
 
Example #29
Source File: UsingVarargsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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) {}
}
 
Example #30
Source File: DeprecatedStubbingTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldStubbingBeTreatedAsInteraction() throws Exception {
    stub(mock.booleanReturningMethod()).toReturn(true);
    
    mock.booleanReturningMethod();
    
    try {
        verifyNoMoreInteractions(mock);
        fail();
    } catch (NoInteractionsWanted e) {}
}