Search in sources :

Example 6 with PrivateMethodDemo

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

the class PrivateMethodDemoTest method testMockPrivateMethod.

@Test
public void testMockPrivateMethod() throws Exception {
    PrivateMethodDemo tested = createPartialMock(PrivateMethodDemo.class, "sayIt", String.class);
    String expected = "Hello altered World";
    expectPrivate(tested, "sayIt", "name").andReturn(expected);
    replay(tested);
    String actual = tested.say("name");
    verify(tested);
    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 7 with PrivateMethodDemo

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

the class PrivateMethodDemoTest method testMethodCallingPrimitiveTestMethod.

@Test
public void testMethodCallingPrimitiveTestMethod() throws Exception {
    PrivateMethodDemo tested = createPartialMock(PrivateMethodDemo.class, "aTestMethod", int.class);
    final int expected = 42;
    expectPrivate(tested, "aTestMethod", new Class<?>[] { int.class }, 10).andReturn(expected);
    replay(tested);
    final int actual = tested.methodCallingPrimitiveTestMethod();
    verify(tested);
    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 8 with PrivateMethodDemo

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

the class PrivateMethodDemoTest method testMethodCallingWrappedTestMethod.

@Test
public void testMethodCallingWrappedTestMethod() throws Exception {
    PrivateMethodDemo tested = createPartialMock(PrivateMethodDemo.class, "aTestMethod", Integer.class);
    final int expected = 42;
    expectPrivate(tested, "aTestMethod", new Class<?>[] { Integer.class }, new Integer(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) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with PrivateMethodDemo

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

the class PrivateInstanceMockingTest method expectationsWorkWhenSpyingOnPrivateMethods.

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

Example 10 with PrivateMethodDemo

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

the class PrivateInstanceMockingTest method expectationsWorkWithArgumentMatchersWhenSpyingOnPrivateMethods.

@Test
public void expectationsWorkWithArgumentMatchersWhenSpyingOnPrivateMethods() 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));
    verifyPrivate(tested).invoke("doSayYear", 29, "Johan");
    verifyPrivate(tested).invoke("doSayYear", 12, "test");
    verifyPrivate(tested).invoke("doSayYear", 50, "Temp");
}
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