Search in sources :

Example 31 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 32 with PrivateMethodDemo

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

the class PrivateInstanceMockingTest method answersWorkWhenSpyingOnPrivateVoidMethods.

@Test
public void answersWorkWhenSpyingOnPrivateVoidMethods() throws Exception {
    PrivateMethodDemo tested = spy(new PrivateMethodDemo());
    tested.doObjectStuff(new Object());
    when(tested, "doObjectInternal", isA(String.class)).thenAnswer(new Answer<Void>() {

        private static final long serialVersionUID = 20645008237481667L;

        public Void answer(InvocationOnMock invocation) throws Throwable {
            assertEquals("Testing", invocation.getArguments()[0]);
            return null;
        }
    });
    tested.doObjectStuff(new Object());
    tested.doObjectStuff("Testing");
}
Also used : PrivateMethodDemo(samples.privatemocking.PrivateMethodDemo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 33 with PrivateMethodDemo

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

the class PrivateMethodDemoTest method testMockPrivateMethod_withArgument.

@Test
public void testMockPrivateMethod_withArgument() throws Exception {
    PrivateMethodDemo tested = new PrivateMethodDemo();
    String expected = "Hello altered World";
    String actual = Whitebox.invokeMethod(tested, "sayIt", "altered World");
    assertEquals("Expected and actual did not match", expected, actual);
}
Also used : PrivateMethodDemo(samples.privatemocking.PrivateMethodDemo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 34 with PrivateMethodDemo

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

the class PrivateMethodDemoTest method testInvokePrivateMethod.

@Test
public void testInvokePrivateMethod() throws Exception {
    PrivateMethodDemo tested = new PrivateMethodDemo();
    String expected = "Hello world";
    String actual = Whitebox.invokeMethod(tested, "sayIt");
    assertEquals("Expected and actual did not match", expected, actual);
}
Also used : PrivateMethodDemo(samples.privatemocking.PrivateMethodDemo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 35 with PrivateMethodDemo

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

the class PrivateMethodDemoTest method testMethodCallingWrappedTestMethod_reflectiveMethodLookup.

@Test
public void testMethodCallingWrappedTestMethod_reflectiveMethodLookup() throws Exception {
    PrivateMethodDemo tested = createPartialMock(PrivateMethodDemo.class, "aTestMethod", Integer.class);
    final Method methodToExpect = PrivateMethodDemo.class.getDeclaredMethod("aTestMethod", Integer.class);
    final int expected = 42;
    expectPrivate(tested, methodToExpect, 15).andReturn(expected);
    replay(tested);
    final int actual = tested.methodCallingWrappedTestMethod();
    verify(tested);
    assertEquals("Expected and actual did not match", expected, actual);
}
Also used : PrivateMethodDemo(samples.privatemocking.PrivateMethodDemo) Method(java.lang.reflect.Method) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

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