use of samples.privatemocking.PrivateMethodDemo in project powermock by powermock.
the class PrivateInstanceMockingTest method expectationsWorkWhenSpyingOnPrivateMethodsUsingDoReturn.
@Test
public void expectationsWorkWhenSpyingOnPrivateMethodsUsingDoReturn() throws Exception {
PrivateMethodDemo tested = spy(new PrivateMethodDemo());
assertEquals("Hello Temp, you are 50 old.", tested.sayYear("Temp", 50));
doReturn("another").when(tested, "doSayYear", 12, "test");
assertEquals("Hello Johan, you are 29 old.", tested.sayYear("Johan", 29));
assertEquals("another", tested.sayYear("test", 12));
verifyPrivate(tested).invoke("doSayYear", 12, "test");
}
use of samples.privatemocking.PrivateMethodDemo in project powermock by powermock.
the class PrivateInstanceMockingTest method errorousVerificationOnPrivateMethodGivesFilteredErrorMessage.
@Test
public void errorousVerificationOnPrivateMethodGivesFilteredErrorMessage() throws Exception {
PrivateMethodDemo tested = spy(new PrivateMethodDemo());
assertEquals("Hello Temp, you are 50 old.", tested.sayYear("Temp", 50));
when(tested, "doSayYear", Mockito.anyInt(), Mockito.anyString()).thenReturn("another");
assertEquals("another", tested.sayYear("Johan", 29));
assertEquals("another", tested.sayYear("test", 12));
try {
verifyPrivate(tested, never()).invoke("doSayYear", 50, "Temp");
fail("Should throw assertion error");
} catch (MockitoAssertionError e) {
assertEquals("\nsamples.privatemocking.PrivateMethodDemo.doSayYear(\n 50,\n \"Temp\"\n);\nNever wanted but invoked .", e.getMessage());
}
}
use of samples.privatemocking.PrivateMethodDemo in project powermock by powermock.
the class PrivateInstanceMockingTest method expectationsWorkWhenSpyingOnPrivateVoidMethods.
@Test(expected = ArrayStoreException.class)
public void expectationsWorkWhenSpyingOnPrivateVoidMethods() throws Exception {
PrivateMethodDemo tested = spy(new PrivateMethodDemo());
tested.doObjectStuff(new Object());
when(tested, "doObjectInternal", isA(Object.class)).thenThrow(new ArrayStoreException());
tested.doObjectStuff(new Object());
}
use of samples.privatemocking.PrivateMethodDemo in project powermock by powermock.
the class PrivateInstanceMockingTest method errorousVerificationOnPrivateMethodGivesFilteredErrorMessage.
@Test
public void errorousVerificationOnPrivateMethodGivesFilteredErrorMessage() throws Exception {
PrivateMethodDemo tested = spy(new PrivateMethodDemo());
assertEquals("Hello Temp, you are 50 old.", tested.sayYear("Temp", 50));
when(tested, "doSayYear", Mockito.anyInt(), Mockito.anyString()).thenReturn("another");
assertEquals("another", tested.sayYear("Johan", 29));
assertEquals("another", tested.sayYear("test", 12));
try {
verifyPrivate(tested, never()).invoke("doSayYear", 50, "Temp");
fail("Should throw assertion error");
} catch (MockitoAssertionError e) {
assertEquals("\nsamples.privatemocking.PrivateMethodDemo.doSayYear(\n 50,\n \"Temp\"\n);\nNever wanted but invoked .", e.getMessage());
}
}
use of samples.privatemocking.PrivateMethodDemo in project powermock by powermock.
the class PrivateInstanceMockingTest method expectationsWorkWhenSpyingOnPrivateVoidMethods.
@Test(expected = ArrayStoreException.class)
public void expectationsWorkWhenSpyingOnPrivateVoidMethods() throws Exception {
PrivateMethodDemo tested = spy(new PrivateMethodDemo());
tested.doObjectStuff(new Object());
when(tested, "doObjectInternal", isA(Object.class)).thenThrow(new ArrayStoreException());
tested.doObjectStuff(new Object());
}
Aggregations