org.mockito.exceptions.misusing.NotAMockException Java Examples

The following examples show how to use org.mockito.exceptions.misusing.NotAMockException. 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: ValidatingPoolableLdapConnectionFactoryTest.java    From directory-ldap-api with Apache License 2.0 7 votes vote down vote up
private static final LdapConnection verify( LdapConnection connection, VerificationMode mode )
{
    if ( MockUtil.isMock( connection ) )
    {
        return org.mockito.Mockito.verify( connection, mode );
    }
    else
    {
        if ( connection instanceof Wrapper )
        {
            @SuppressWarnings("unchecked")
            LdapConnection unwrapped = ( ( Wrapper<LdapConnection> ) connection ).wrapped();
            return verify( unwrapped, mode );
        }
    }
    throw new NotAMockException( "connection is not a mock, nor a wrapper for a connection that is one" );
}
 
Example #2
Source File: MockitoCore.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void verifyNoMoreInteractions(Object... mocks) {
    assertMocksNotEmpty(mocks);
    mockingProgress.validateState();
    for (Object mock : mocks) {
        try {
            if (mock == null) {
                reporter.nullPassedToVerifyNoMoreInteractions();
            }
            InvocationContainer invocations = mockUtil.getMockHandler(mock).getInvocationContainer();
            VerificationDataImpl data = new VerificationDataImpl(invocations, null);
            VerificationModeFactory.noMoreInteractions().verify(data);
        } catch (NotAMockException e) {
            reporter.notAMockPassedToVerifyNoMoreInteractions();
        }
    }
}
 
Example #3
Source File: MockitoCore.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void verifyNoMoreInteractions(Object... mocks) {
    assertMocksNotEmpty(mocks);
    mockingProgress.validateState();
    for (Object mock : mocks) {
        try {
            if (mock == null) {
                reporter.nullPassedToVerifyNoMoreInteractions();
            }
            InvocationContainer invocations = mockUtil.getMockHandler(mock).getInvocationContainer();
            VerificationDataImpl data = new VerificationDataImpl(invocations, null);
            VerificationModeFactory.noMoreInteractions().verify(data);
        } catch (NotAMockException e) {
            reporter.notAMockPassedToVerifyNoMoreInteractions();
        }
    }
}
 
Example #4
Source File: Reporter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void notAMockPassedToVerify(Class type) {
    throw new NotAMockException(join(
            "Argument passed to verify() is of type " + type.getSimpleName() + " and is not a mock!",
            "Make sure you place the parenthesis correctly!",
            "See the examples of correct verifications:",
            "    verify(mock).someMethod();",
            "    verify(mock, times(10)).someMethod();",
            "    verify(mock, atLeastOnce()).someMethod();"
    ));
}
 
Example #5
Source File: MockitoMisusingUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenNotASpy_whenDoReturn_thenThrowNotAMock() {
    try {
        List<String> list = new ArrayList<String>();

        Mockito.doReturn(100, Mockito.withSettings().lenient())
            .when(list)
            .size();

        fail("Should have thrown a NotAMockException because 'list' is not a mock!");
    } catch (NotAMockException e) {
        assertThat(e.getMessage(), containsString("Argument passed to when() is not a mock!"));
    }
}
 
Example #6
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 #7
Source File: MockUtilTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test 
public void should_scream_when_enhanced_but_not_a_mock_passed() {
    Object o = Enhancer.create(ArrayList.class, NoOp.INSTANCE);
    try {
        mockUtil.getMockHandler(o);
        fail();
    } catch (NotAMockException e) {}
}
 
Example #8
Source File: MockUtil.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public <T> InternalMockHandler<T> getMockHandler(T mock) {
    if (mock == null) {
        throw new NotAMockException("Argument should be a mock, but is null!");
    }

    if (isMockitoMock(mock)) {
        MockHandler handler = mockMaker.getHandler(mock);
        return (InternalMockHandler) handler;
    } else {
        throw new NotAMockException("Argument should be a mock, but is: " + mock.getClass());
    }
}
 
Example #9
Source File: MockUtilTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test 
public void shouldScreamWhenEnhancedButNotAMockPassed() {
    Object o = Enhancer.create(ArrayList.class, NoOp.INSTANCE);
    try {
        mockUtil.getMockHandler(o);
        fail();
    } catch (NotAMockException e) {}
}
 
Example #10
Source File: Reporter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void notAMockPassedToWhenMethod() {
    throw new NotAMockException(join(
            "Argument passed to when() is not a mock!",
            "Example of correct stubbing:",
            "    doThrow(new RuntimeException()).when(mock).someMethod();"
    ));
}
 
Example #11
Source File: Reporter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void notAMockPassedToVerifyNoMoreInteractions() {
    throw new NotAMockException(join(
        "Argument(s) passed is not a mock!",
        "Examples of correct verifications:",
        "    verifyNoMoreInteractions(mockOne, mockTwo);",
        "    verifyZeroInteractions(mockOne, mockTwo);",
        ""
    ));
}
 
Example #12
Source File: MockUtil.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public <T> MockHandlerInterface<T> getMockHandler(T mock) {
    if (mock == null) {
        throw new NotAMockException("Argument should be a mock, but is null!");
    }

    if (isMockitoMock(mock)) {
        return (MockHandlerInterface) getInterceptor(mock).getHandler();
    } else {
        throw new NotAMockException("Argument should be a mock, but is: " + mock.getClass());
    }
}
 
Example #13
Source File: Reporter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void notAMockPassedWhenCreatingInOrder() {
    throw new NotAMockException(join(
            "Argument(s) passed is not a mock!",
            "Pass mocks that require verification in order.",
            "For example:",
            "    InOrder inOrder = inOrder(mockOne, mockTwo);"
            ));
}
 
Example #14
Source File: DescriptiveMessagesOnMisuseTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected=NotAMockException.class)
public void shouldScreamWhenWholeMethodPassedToVerifyNoMoreInteractions() {
    verifyNoMoreInteractions(mock.byteReturningMethod());
}
 
Example #15
Source File: MockitoTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected=NotAMockException.class)
public void shouldValidateMockWhenCreatingInOrderObject() {
    Mockito.inOrder("notMock");
}
 
Example #16
Source File: MockitoTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
@Test(expected=NotAMockException.class)
public void shouldValidateMockWhenStubbingVoid() {
    Mockito.stubVoid("notMock");
}
 
Example #17
Source File: MockitoTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected=NotAMockException.class)
public void shouldValidateMockWhenVerifyingZeroInteractions() {
    Mockito.verifyZeroInteractions("notMock");
}
 
Example #18
Source File: MockitoTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected=NotAMockException.class)
public void shouldValidateMockWhenVerifyingNoMoreInteractions() {
    Mockito.verifyNoMoreInteractions("notMock");
}
 
Example #19
Source File: MockitoTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected=NotAMockException.class)
public void shouldValidateMockWhenVerifyingWithExpectedNumberOfInvocations() {
    Mockito.verify("notMock", times(19));
}
 
Example #20
Source File: MockitoTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected=NotAMockException.class)
public void shouldValidateMockWhenVerifying() {
    Mockito.verify("notMock");
}
 
Example #21
Source File: MockUtilTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test (expected=NotAMockException.class)
public void should_scream_when_not_a_mock_passed() {
    mockUtil.getMockHandler("");
}
 
Example #22
Source File: VerificationExcludingStubsTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected = NotAMockException.class)
public void shouldIgnoringStubsDetectNonMocks() throws Exception {
    ignoreStubs(mock, new Object());
}
 
Example #23
Source File: VerificationExcludingStubsTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected = NotAMockException.class)
public void shouldIgnoringStubsDetectNulls() throws Exception {
    ignoreStubs(mock, null);
}
 
Example #24
Source File: ResetTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected = NotAMockException.class)
public void resettingNullIsSafe() {
    reset(new Object[] {null});
}
 
Example #25
Source File: ResetTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected = NotAMockException.class)
public void resettingNonMockIsSafe() {
    reset("");
}
 
Example #26
Source File: DescriptiveMessagesOnMisuseTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected=NotAMockException.class)
public void shouldScreamWhenInOrderCreatedWithDodgyMock() {
    inOrder("not a mock");
}
 
Example #27
Source File: DescriptiveMessagesOnMisuseTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected=NotAMockException.class)
public void shouldScreamWhenInOrderCreatedWithDodgyMock() {
    inOrder("not a mock");
}
 
Example #28
Source File: DescriptiveMessagesOnMisuseTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected=NotAMockException.class)
public void shouldScreamWhenWholeMethodPassedToVerify() {
    verify(mock.booleanReturningMethod());
}
 
Example #29
Source File: BDDMockitoTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected = NotAMockException.class)
public void shouldValidateMockWhenVerifyingNoMoreInteractions() {

    then("notMock").should();
}
 
Example #30
Source File: BDDMockitoTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected = NotAMockException.class)
public void shouldValidateMockWhenVerifyingWithExpectedNumberOfInvocations() {

    then("notMock").should(times(19));
}