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