Search in sources :

Example 21 with PrivateMethodDemo

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());
    }
}
Also used : PrivateMethodDemo(samples.privatemocking.PrivateMethodDemo) MockitoAssertionError(org.mockito.exceptions.base.MockitoAssertionError) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 22 with PrivateMethodDemo

use of samples.privatemocking.PrivateMethodDemo in project powermock by powermock.

the class PrivateInstanceMockingTest method verifyPrivateMethodWhenNoExpectationForTheMethodHasBeenMade.

@Test
public void verifyPrivateMethodWhenNoExpectationForTheMethodHasBeenMade() throws Exception {
    PrivateMethodDemo tested = spy(new PrivateMethodDemo());
    assertEquals("Hello Johan, you are 29 old.", tested.sayYear("Johan", 29));
    verifyPrivate(tested).invoke("doSayYear", 29, "Johan");
}
Also used : PrivateMethodDemo(samples.privatemocking.PrivateMethodDemo) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 23 with PrivateMethodDemo

use of samples.privatemocking.PrivateMethodDemo in project powermock by powermock.

the class PrivateInstanceMockingTest method usingMultipleArgumentsOnPrivateMethodWorks.

@Test
public void usingMultipleArgumentsOnPrivateMethodWorks() throws Exception {
    File file = mock(File.class);
    FileDataSource fileDataSource = mock(FileDataSource.class);
    StringReader expected = new StringReader("Some string");
    PrivateMethodDemo tested = mock(PrivateMethodDemo.class);
    doReturn(expected).when(tested, method(PrivateMethodDemo.class, "createReader", File.class, FileDataSource.class)).withArguments(file, fileDataSource);
    StringReader actual = Whitebox.invokeMethod(tested, "createReader", file, fileDataSource);
    assertSame(expected, actual);
}
Also used : PrivateMethodDemo(samples.privatemocking.PrivateMethodDemo) FileDataSource(javax.activation.FileDataSource) StringReader(java.io.StringReader) File(java.io.File) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 24 with PrivateMethodDemo

use of samples.privatemocking.PrivateMethodDemo in project powermock by powermock.

the class PrivateInstanceMockingTest method expectationsWorkWhenSpyingOnPrivateMethodsUsingDoReturnWhenMethodDoesntHaveAnyArguments.

@Test
public void expectationsWorkWhenSpyingOnPrivateMethodsUsingDoReturnWhenMethodDoesntHaveAnyArguments() throws Exception {
    PrivateMethodDemo tested = spy(new PrivateMethodDemo());
    doReturn("another").when(tested, "sayIt");
    assertEquals("another", Whitebox.invokeMethod(tested, "sayIt"));
    verifyPrivate(tested).invoke("sayIt");
}
Also used : PrivateMethodDemo(samples.privatemocking.PrivateMethodDemo) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 25 with PrivateMethodDemo

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());
}
Also used : PrivateMethodDemo(samples.privatemocking.PrivateMethodDemo) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Test (org.junit.Test)35 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)35 PrivateMethodDemo (samples.privatemocking.PrivateMethodDemo)35 MockitoAssertionError (org.mockito.exceptions.base.MockitoAssertionError)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 File (java.io.File)1 StringReader (java.io.StringReader)1 Method (java.lang.reflect.Method)1 FileDataSource (javax.activation.FileDataSource)1