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();
}
}
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();
}
}
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();
}
}
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));
}
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());
}
Aggregations