use of org.mockito.MockitoSession in project dexmaker by linkedin.
the class VerifyStatic method verifyInOrder.
@Test
public void verifyInOrder() throws Exception {
MockitoSession session = mockitoSession().mockStatic(EchoClass.class).mockStatic(ConsumeClass.class).startMocking();
try {
EchoClass.echo("marco!");
ConsumeClass.consume("donuts");
ConsumeClass.consume("nougat");
EchoClass.echo("polo");
StaticInOrder echoInOrder = inOrder(staticMockMarker(EchoClass.class));
echoInOrder.verify(() -> EchoClass.echo(eq("marco!")));
echoInOrder.verify(() -> EchoClass.echo(eq("polo")));
echoInOrder.verifyNoMoreInteractions();
StaticInOrder consumeInOrder = inOrder(staticMockMarker(ConsumeClass.class));
consumeInOrder.verify(() -> ConsumeClass.consume(eq("donuts")));
consumeInOrder.verify(() -> ConsumeClass.consume(eq("nougat")));
consumeInOrder.verifyNoMoreInteractions();
StaticInOrder combinedInOrder = inOrder(staticMockMarker(EchoClass.class, ConsumeClass.class));
combinedInOrder.verify(() -> EchoClass.echo(eq("marco!")));
combinedInOrder.verify(() -> ConsumeClass.consume(eq("donuts")));
combinedInOrder.verify(() -> ConsumeClass.consume(eq("nougat")));
combinedInOrder.verify(() -> EchoClass.echo(eq("polo")));
combinedInOrder.verifyNoMoreInteractions();
} finally {
session.finishMocking();
}
}
use of org.mockito.MockitoSession in project dexmaker by linkedin.
the class MockStatic method mockSuperMethod.
@Test
public void mockSuperMethod() throws Exception {
MockitoSession session = mockitoSession().mockStatic(SuperClass.class).startMocking();
try {
// By default all static methods of the mocked class should return the default answers
assertNull(SuperClass.returnB());
assertNull(SuperClass.returnC());
// Sub class should not be affected
assertEquals("superB", SubClass.returnB());
assertEquals("subC", SubClass.returnC());
when(SuperClass.returnB()).thenReturn("fakeB");
when(SuperClass.returnC()).thenReturn("fakeC");
// Make sure behavior is changed
assertEquals("fakeB", SuperClass.returnB());
assertEquals("fakeC", SuperClass.returnC());
// Sub class should not be affected
assertEquals("superB", SubClass.returnB());
assertEquals("subC", SubClass.returnC());
} finally {
session.finishMocking();
}
// Mocking should be stopped
assertEquals("superB", SuperClass.returnB());
assertEquals("superC", SuperClass.returnC());
}
use of org.mockito.MockitoSession in project dexmaker by linkedin.
the class MockStatic method spyStatic.
@Test
public void spyStatic() throws Exception {
ContentResolver resolver = InstrumentationRegistry.getTargetContext().getContentResolver();
String deviceName = Settings.Global.getString(resolver, DEVICE_NAME);
MockitoSession session = mockitoSession().spyStatic(Settings.Global.class).startMocking();
try {
// Cannot call when(Settings.getString(any(ContentResolver.class), eq("...")))
// as any(ContentResolver.class) returns null which makes getString fail. Hence need to
// use less lambda API
doReturn("23").when(() -> Settings.Global.getString(any(ContentResolver.class), eq("twenty three")));
doReturn(42).when(() -> Settings.Global.getInt(any(ContentResolver.class), eq("fourty two")));
// Make sure behavior is changed
assertEquals("23", Settings.Global.getString(resolver, "twenty three"));
assertEquals(42, Settings.Global.getInt(resolver, "fourty two"));
// Make sure non-mocked methods work as before
assertEquals(deviceName, Settings.Global.getString(resolver, DEVICE_NAME));
} finally {
session.finishMocking();
}
}
use of org.mockito.MockitoSession in project dexmaker by linkedin.
the class MockStatic method onlyOneMethodCallDuringStubbing.
@Test
public void onlyOneMethodCallDuringStubbing() throws Exception {
MockitoSession session = mockitoSession().strictness(Strictness.LENIENT).spyStatic(SuperClass.class).startMocking();
try {
try {
doReturn("").when(() -> {
SuperClass.returnB();
SuperClass.returnC();
});
fail();
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage(), e.getMessage().contains("returnB"));
assertTrue(e.getMessage(), e.getMessage().contains("returnC"));
assertFalse(e.getMessage(), e.getMessage().contains("returnA"));
}
} finally {
session.finishMocking();
}
}
use of org.mockito.MockitoSession in project dexmaker by linkedin.
the class MockStatic method verifyMockingDetails.
@Test
public void verifyMockingDetails() throws Exception {
MockitoSession session = mockitoSession().mockStatic(SuperClass.class).spyStatic(SubClass.class).startMocking();
try {
when(SuperClass.returnB()).thenReturn("fakeB");
SuperClass.returnB();
SuperClass.returnC();
MockingDetails superClassDetails = mockingDetails(staticMockMarker(SuperClass.class));
assertTrue(superClassDetails.isMock());
assertFalse(superClassDetails.isSpy());
assertEquals(2, superClassDetails.getInvocations().size());
assertEquals(1, superClassDetails.getStubbings().size());
MockingDetails subClassDetails = mockingDetails(staticMockMarker(SubClass.class));
assertTrue(subClassDetails.isMock());
assertTrue(subClassDetails.isSpy());
} finally {
session.finishMocking();
}
}
Aggregations