Package samples.privatemocking

Examples of samples.privatemocking.PrivateMethodDemo


        tested.doObjectStuff(new Object());
    }

    @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");
    }
View Full Code Here


        verifyPrivate(tested).invoke("doSayYear", 50, "Temp");
    }

    @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) {
View Full Code Here

        }
    }

    @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());
    }
View Full Code Here

        tested.doObjectStuff(new Object());
    }

    @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");
    }
View Full Code Here

@PrepareForTest(PrivateMethodDemo.class)
public class PrivateMethodDemoTest {

  @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);
  }
View Full Code Here

    assertEquals("Expected and actual did not match", expected, actual);
  }

  @Test
  public void testMockPrivateMethod_withArgument() throws Exception {
    PrivateMethodDemo tested = new PrivateMethodDemo();
    String expected = "Hello altered World";

    String actual = (String) Whitebox.invokeMethod(tested, "sayIt",
        "altered World");
View Full Code Here

  }

  @Test
  public void testInvokePrivateMethod() throws Exception {

    PrivateMethodDemo tested = new PrivateMethodDemo();
    String expected = "Hello world";

    String actual = (String) Whitebox.invokeMethod(tested, "sayIt");

    assertEquals("Expected and actual did not match", expected, actual);
View Full Code Here

    assertEquals("Expected and actual did not match", expected, actual);
  }

  @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);
  }
View Full Code Here

    assertEquals("Expected and actual did not match", expected, actual);
  }

  @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);
  }
View Full Code Here

  }

  @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);
  }
View Full Code Here

TOP

Related Classes of samples.privatemocking.PrivateMethodDemo

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.