Search in sources :

Example 1 with NoInteractionsWanted

use of org.mockito.exceptions.verification.NoInteractionsWanted in project dexmaker by linkedin.

the class GeneralMocking method verifyAdditionalInvocations.

@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()));
        }
    }
}
Also used : NoInteractionsWanted(org.mockito.exceptions.verification.NoInteractionsWanted) MockitoException(org.mockito.exceptions.base.MockitoException) Test(org.junit.Test)

Example 2 with NoInteractionsWanted

use of org.mockito.exceptions.verification.NoInteractionsWanted in project mockito by mockito.

the class SelectedMocksInOrderVerificationTest method shouldAllowTwoTimesOnMockTwo.

@Test
public void shouldAllowTwoTimesOnMockTwo() {
    InOrder inOrder = inOrder(mockTwo, mockThree);
    inOrder.verify(mockTwo, times(2)).simpleMethod(2);
    try {
        verifyNoMoreInteractions(mockTwo);
        fail();
    } catch (NoInteractionsWanted e) {
    }
}
Also used : InOrder(org.mockito.InOrder) NoInteractionsWanted(org.mockito.exceptions.verification.NoInteractionsWanted) Test(org.junit.Test)

Example 3 with NoInteractionsWanted

use of org.mockito.exceptions.verification.NoInteractionsWanted in project mockito by mockito.

the class Reporter method noMoreInteractionsWanted.

public static MockitoAssertionError noMoreInteractionsWanted(Invocation undesired, List<VerificationAwareInvocation> invocations) {
    ScenarioPrinter scenarioPrinter = new ScenarioPrinter();
    String scenario = scenarioPrinter.print(invocations);
    return new NoInteractionsWanted(join("No interactions wanted here:", new LocationImpl(), "But found this interaction on mock '" + MockUtil.getMockName(undesired.getMock()) + "':", undesired.getLocation(), scenario));
}
Also used : NoInteractionsWanted(org.mockito.exceptions.verification.NoInteractionsWanted) LocationImpl(org.mockito.internal.debugging.LocationImpl) ScenarioPrinter(org.mockito.internal.exceptions.util.ScenarioPrinter)

Example 4 with NoInteractionsWanted

use of org.mockito.exceptions.verification.NoInteractionsWanted in project mockito by mockito.

the class Reporter method noInteractionsWanted.

public static MockitoAssertionError noInteractionsWanted(Object mock, List<VerificationAwareInvocation> invocations) {
    ScenarioPrinter scenarioPrinter = new ScenarioPrinter();
    String scenario = scenarioPrinter.print(invocations);
    List<Location> locations = new ArrayList<>();
    for (VerificationAwareInvocation invocation : invocations) {
        locations.add(invocation.getLocation());
    }
    return new NoInteractionsWanted(join("No interactions wanted here:", new LocationImpl(), "But found these interactions on mock '" + MockUtil.getMockName(mock) + "':", join("", locations), scenario));
}
Also used : NoInteractionsWanted(org.mockito.exceptions.verification.NoInteractionsWanted) LocationImpl(org.mockito.internal.debugging.LocationImpl) ScenarioPrinter(org.mockito.internal.exceptions.util.ScenarioPrinter) Location(org.mockito.invocation.Location)

Example 5 with NoInteractionsWanted

use of org.mockito.exceptions.verification.NoInteractionsWanted in project mockito by mockito.

the class NoInteractionsTest method noInteractionsExceptionMessageShouldDescribeMock.

@Test
public void noInteractionsExceptionMessageShouldDescribeMock() {
    // given
    NoInteractions n = new NoInteractions();
    IMethods mock = mock(IMethods.class, "a mock");
    InvocationMatcher i = new InvocationBuilder().mock(mock).toInvocationMatcher();
    InvocationContainerImpl invocations = new InvocationContainerImpl(new MockSettingsImpl());
    invocations.setInvocationForPotentialStubbing(i);
    try {
        // when
        n.verify(new VerificationDataImpl(invocations, null));
        // then
        fail();
    } catch (NoInteractionsWanted e) {
        Assertions.assertThat(e.toString()).contains(mock.toString());
    }
}
Also used : InvocationContainerImpl(org.mockito.internal.stubbing.InvocationContainerImpl) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) NoInteractionsWanted(org.mockito.exceptions.verification.NoInteractionsWanted) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) IMethods(org.mockitousage.IMethods) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Aggregations

NoInteractionsWanted (org.mockito.exceptions.verification.NoInteractionsWanted)7 Test (org.junit.Test)5 MockSettingsImpl (org.mockito.internal.creation.MockSettingsImpl)2 LocationImpl (org.mockito.internal.debugging.LocationImpl)2 ScenarioPrinter (org.mockito.internal.exceptions.util.ScenarioPrinter)2 InvocationBuilder (org.mockito.internal.invocation.InvocationBuilder)2 InvocationMatcher (org.mockito.internal.invocation.InvocationMatcher)2 InvocationContainerImpl (org.mockito.internal.stubbing.InvocationContainerImpl)2 IMethods (org.mockitousage.IMethods)2 InOrder (org.mockito.InOrder)1 MockitoException (org.mockito.exceptions.base.MockitoException)1 Invocation (org.mockito.invocation.Invocation)1 Location (org.mockito.invocation.Location)1