Search in sources :

Example 1 with MockitoSession

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

the class MockStatic method doReturnTwice.

@Test
public void doReturnTwice() throws Exception {
    MockitoSession session = mockitoSession().mockStatic(SuperClass.class).startMocking();
    try {
        doReturn("fakeB").doReturn("fakeB2").when(SuperClass::returnB);
        assertEquals("fakeB", SuperClass.returnB());
        assertEquals("fakeB2", SuperClass.returnB());
    } finally {
        session.finishMocking();
    }
}
Also used : MockitoSession(org.mockito.MockitoSession) Test(org.junit.Test)

Example 2 with MockitoSession

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

the class MockStatic method resetMock.

@Test
public void resetMock() throws Exception {
    MockitoSession session = mockitoSession().mockStatic(SuperClass.class).startMocking();
    try {
        assertNull(SuperClass.returnB());
        when(SuperClass.returnB()).thenReturn("fakeB");
        assertEquals("fakeB", SuperClass.returnB());
        reset(staticMockMarker(SuperClass.class));
        assertNull(SuperClass.returnB());
    } finally {
        session.finishMocking();
    }
}
Also used : MockitoSession(org.mockito.MockitoSession) Test(org.junit.Test)

Example 3 with MockitoSession

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

the class MockStatic method mockWithTwoClasses.

@Test
public void mockWithTwoClasses() throws Exception {
    MockitoSession session = mockitoSession().mockStatic(SuperClass.class).mockStatic(SubClass.class).startMocking();
    try {
        when(SuperClass.returnB()).thenReturn("fakeB");
        assertEquals("fakeB", SuperClass.returnB());
        when(SubClass.returnC()).thenReturn("fakeC");
        assertEquals("fakeC", SubClass.returnC());
    } finally {
        session.finishMocking();
    }
}
Also used : MockitoSession(org.mockito.MockitoSession) Test(org.junit.Test)

Example 4 with MockitoSession

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

the class MockStatic method mockStatic.

@Test
public void mockStatic() throws Exception {
    ContentResolver resolver = InstrumentationRegistry.getTargetContext().getContentResolver();
    String deviceName = Settings.Global.getString(resolver, DEVICE_NAME);
    MockitoSession session = mockitoSession().mockStatic(Settings.Global.class).startMocking();
    try {
        // By default all static methods of the mocked class should return null/0/false
        assertNull(Settings.Global.getString(resolver, DEVICE_NAME));
        when(Settings.Global.getString(any(ContentResolver.class), eq(DEVICE_NAME))).thenReturn("This is a test");
        // Make sure behavior is changed
        assertEquals("This is a test", Settings.Global.getString(resolver, DEVICE_NAME));
    } finally {
        session.finishMocking();
    }
    // Once the mocking is removed, the behavior should be back to normal
    assertEquals(deviceName, Settings.Global.getString(resolver, DEVICE_NAME));
}
Also used : MockitoSession(org.mockito.MockitoSession) ContentResolver(android.content.ContentResolver) Test(org.junit.Test)

Example 5 with MockitoSession

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

the class MockStatic method staticMockingIsSeparateFromNonStaticMocking.

@Test
public void staticMockingIsSeparateFromNonStaticMocking() throws Exception {
    SuperClass objA = new SuperClass();
    SuperClass objB;
    MockitoSession session = mockitoSession().mockStatic(SuperClass.class).startMocking();
    try {
        assertNull(SuperClass.returnB());
        assertNull(objA.returnB());
        objB = mock(SuperClass.class);
        assertEquals("superA", objA.returnA());
        // Any kind of static method method call should be mocked
        assertNull(objB.returnA());
        assertNull(SuperClass.returnB());
        assertNull(objA.returnB());
        assertNull(objB.returnB());
    } finally {
        session.finishMocking();
    }
    assertEquals("superA", objA.returnA());
    assertNull(objB.returnA());
    // Any kind of static method method call should _not_ be mocked
    assertEquals("superB", SuperClass.returnB());
    assertEquals("superB", objA.returnB());
    assertEquals("superB", objB.returnB());
}
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