Search in sources :

Example 11 with MockitoSession

use of org.mockito.MockitoSession in project dexmaker by linkedin.

the class VerifyStatic method verifyWithIgnoreStubs.

@Test
public void verifyWithIgnoreStubs() throws Exception {
    MockitoSession session = mockitoSession().spyStatic(EchoClass.class).startMocking();
    try {
        // 'ignoreStubs' only ignore stubs
        when(EchoClass.echo("marco!")).thenReturn("polo");
        assertEquals("polo", EchoClass.echo("marco!"));
        assertEquals("echo", EchoClass.echo("echo"));
        verify(() -> {
            return EchoClass.echo(eq("echo"));
        });
        verifyNoMoreInteractions(ignoreStubs(staticMockMarker(EchoClass.class)));
    } finally {
        session.finishMocking();
    }
}
Also used : MockitoSession(org.mockito.MockitoSession) Test(org.junit.Test)

Example 12 with MockitoSession

use of org.mockito.MockitoSession in project dexmaker by linkedin.

the class VerifyStatic method verifyBadOrder.

@Test(expected = VerificationInOrderFailure.class)
public void verifyBadOrder() throws Exception {
    MockitoSession session = mockitoSession().mockStatic(EchoClass.class).startMocking();
    try {
        EchoClass.echo("marco!");
        EchoClass.echo("polo");
        StaticInOrder echoInOrder = inOrder(staticMockMarker(EchoClass.class));
        echoInOrder.verify(() -> EchoClass.echo(eq("polo")));
        echoInOrder.verify(() -> EchoClass.echo(eq("marco!")));
    } finally {
        session.finishMocking();
    }
}
Also used : MockitoSession(org.mockito.MockitoSession) StaticInOrder(com.android.dx.mockito.inline.extended.StaticInOrder) Test(org.junit.Test)

Example 13 with MockitoSession

use of org.mockito.MockitoSession in project mockito by mockito.

the class DefaultMockitoSessionBuilderTest method uses_logger_and_strictness.

@Test
public void uses_logger_and_strictness() {
    TestClass testClass = new TestClass();
    final List<String> hints = new ArrayList<String>();
    MockitoSession session = new DefaultMockitoSessionBuilder().initMocks(testClass).strictness(WARN).logger(new MockitoSessionLogger() {

        @Override
        public void log(String hint) {
            hints.add(hint);
        }
    }).startMocking();
    when(testClass.set.add(1)).thenReturn(true);
    session.finishMocking();
    assertFalse(hints.isEmpty());
}
Also used : MockitoSession(org.mockito.MockitoSession) MockitoSessionLogger(org.mockito.session.MockitoSessionLogger) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 14 with MockitoSession

use of org.mockito.MockitoSession in project mockito by mockito.

the class MockitoExtension method beforeEach.

/**
 * Callback that is invoked <em>before</em> each test is invoked.
 *
 * @param context the current extension context; never {@code null}
 */
@Override
public void beforeEach(final ExtensionContext context) {
    List<Object> testInstances = context.getRequiredTestInstances().getAllInstances();
    Strictness actualStrictness = this.retrieveAnnotationFromTestClasses(context).map(MockitoSettings::strictness).orElse(strictness);
    MockitoSession session = Mockito.mockitoSession().initMocks(testInstances.toArray()).strictness(actualStrictness).logger(new MockitoSessionLoggerAdapter(Plugins.getMockitoLogger())).startMocking();
    context.getStore(MOCKITO).put(MOCKS, new HashSet<>());
    context.getStore(MOCKITO).put(SESSION, session);
}
Also used : MockitoSession(org.mockito.MockitoSession) MockitoSessionLoggerAdapter(org.mockito.internal.session.MockitoSessionLoggerAdapter) Strictness(org.mockito.quality.Strictness)

Example 15 with MockitoSession

use of org.mockito.MockitoSession in project dexmaker by linkedin.

the class MockStatic method doReturnSpyHasNoSideEffect.

@Test
public void doReturnSpyHasNoSideEffect() throws Exception {
    MockitoSession session = mockitoSession().spyStatic(SubClass.class).startMocking();
    try {
        SubClass.recorded = null;
        SubClass.record("no sideeffect");
        assertEquals("no sideeffect", SubClass.recorded);
        doReturn("faceRecord").when(() -> SubClass.record(eq("test")));
        // Verify that there was no side effect as the lambda gets intercepted
        assertEquals("no sideeffect", SubClass.recorded);
        assertEquals("faceRecord", SubClass.record("test"));
        // Verify that there was no side effect as the method is stubbed
        assertEquals("no sideeffect", SubClass.recorded);
    } finally {
        session.finishMocking();
    }
}
Also used : MockitoSession(org.mockito.MockitoSession) Test(org.junit.Test)

Aggregations

MockitoSession (org.mockito.MockitoSession)31 Test (org.junit.Test)30 StaticInOrder (com.android.dx.mockito.inline.extended.StaticInOrder)3 ContentResolver (android.content.ContentResolver)2 ArrayList (java.util.ArrayList)1 MockingDetails (org.mockito.MockingDetails)1 MissingMethodInvocationException (org.mockito.exceptions.misusing.MissingMethodInvocationException)1 UnnecessaryStubbingException (org.mockito.exceptions.misusing.UnnecessaryStubbingException)1 VerificationInOrderFailure (org.mockito.exceptions.verification.VerificationInOrderFailure)1 MockitoSessionLoggerAdapter (org.mockito.internal.session.MockitoSessionLoggerAdapter)1 Strictness (org.mockito.quality.Strictness)1 MockitoSessionLogger (org.mockito.session.MockitoSessionLogger)1