Search in sources :

Example 16 with MockitoSession

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

the class MockStatic method spyStaticOnObjectWithNoDefaultConstructor.

@Test
public void spyStaticOnObjectWithNoDefaultConstructor() throws Exception {
    new NoDefaultConstructorClass(23);
    MockitoSession session = mockitoSession().spyStatic(NoDefaultConstructorClass.class).startMocking();
    try {
        // Starting the spying hasn't change the static state of the class.
        assertEquals(23, NoDefaultConstructorClass.getLastId());
        when(NoDefaultConstructorClass.getLastId()).thenReturn(42);
        assertEquals(42, NoDefaultConstructorClass.getLastId());
    } finally {
        session.finishMocking();
    }
}
Also used : MockitoSession(org.mockito.MockitoSession) Test(org.junit.Test)

Example 17 with MockitoSession

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

the class MockStatic method mockOverriddenStaticMethod.

@Test
public void mockOverriddenStaticMethod() throws Exception {
    MockitoSession session = mockitoSession().mockStatic(SubClass.class).startMocking();
    try {
        // By default all static methods of the mocked class should return the default answers
        assertNull(SubClass.returnB());
        assertNull(SubClass.returnC());
        // Super class is not mocked
        assertEquals("superB", SuperClass.returnB());
        assertEquals("superC", SuperClass.returnC());
        when(SubClass.returnB()).thenReturn("fakeB");
        when(SubClass.returnC()).thenReturn("fakeC");
        // Make sure behavior is changed
        assertEquals("fakeB", SubClass.returnB());
        assertEquals("fakeC", SubClass.returnC());
        // Super class should not be affected
        assertEquals("superB", SuperClass.returnB());
        assertEquals("superC", SuperClass.returnC());
    } finally {
        session.finishMocking();
    }
    // Mocking should be stopped
    assertEquals("superB", SubClass.returnB());
    assertEquals("subC", SubClass.returnC());
}
Also used : MockitoSession(org.mockito.MockitoSession) Test(org.junit.Test)

Example 18 with MockitoSession

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

the class MockStatic method resetSpy.

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

Example 19 with MockitoSession

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

the class StaticMockitoSession method strictUnnecessaryStubbing.

@Test
public void strictUnnecessaryStubbing() {
    MockitoSession session = mockitoSession().spyStatic(Settings.Global.class).startMocking();
    // Set up unnecessary stubbing
    doReturn("23").when(() -> Settings.Global.getString(any(ContentResolver.class), eq(DEVICE_NAME)));
    try {
        session.finishMocking();
        fail();
    } catch (UnnecessaryStubbingException e) {
        assertTrue("\"" + e.getMessage() + "\" does not contain 'Settings$Global.getString'", e.getMessage().contains("Settings$Global.getString"));
    }
}
Also used : MockitoSession(org.mockito.MockitoSession) UnnecessaryStubbingException(org.mockito.exceptions.misusing.UnnecessaryStubbingException) Test(org.junit.Test)

Example 20 with MockitoSession

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

the class StaticMockitoSession method lenientUnnecessaryStubbing.

@Test
public void lenientUnnecessaryStubbing() {
    MockitoSession session = mockitoSession().strictness(Strictness.LENIENT).spyStatic(Settings.Global.class).startMocking();
    // Set up unnecessary stubbing
    doReturn("23").when(() -> Settings.Global.getString(any(ContentResolver.class), eq(DEVICE_NAME)));
    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