Search in sources :

Example 36 with MyClass

use of samples.newmocking.MyClass in project powermock by powermock.

the class VerifyNoMoreInteractionsTest method verifyNoMoreInteractionsDelegatesToPlainMockitoWhenMockIsNotAPowerMockitoMock.

@Test
public void verifyNoMoreInteractionsDelegatesToPlainMockitoWhenMockIsNotAPowerMockitoMock() throws Exception {
    MyClass myClassMock = Mockito.mock(MyClass.class);
    myClassMock.getMessage();
    try {
        verifyNoMoreInteractions(myClassMock);
        fail("Should throw exception!");
    } catch (AssertionError e) {
        /*
			 * This string would have been deleted by PowerMockito but should
			 * exists if delegation took place.
			 */
        final String expectedTextThatProvesDelegation = "But found this interaction";
        assertTrue(e.getMessage().contains(expectedTextThatProvesDelegation));
    }
}
Also used : MyClass(samples.newmocking.MyClass) MockitoAssertionError(org.mockito.exceptions.base.MockitoAssertionError) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 37 with MyClass

use of samples.newmocking.MyClass in project powermock by powermock.

the class WhenNewCases method testInvokeVoidMethod.

@Test
public void testInvokeVoidMethod() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock = mock(MyClass.class);
    whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock);
    doNothing().when(myClassMock).voidMethod();
    tested.invokeVoidMethod();
    verify(myClassMock).voidMethod();
    verifyNew(MyClass.class).withNoArguments();
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 38 with MyClass

use of samples.newmocking.MyClass in project powermock by powermock.

the class WhenNewCases method testGetMessage.

@Test
public void testGetMessage() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock = mock(MyClass.class);
    whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock);
    String expected = "Hello altered World";
    when(myClassMock.getMessage()).thenReturn("Hello altered World");
    String actual = tested.getMessage();
    verify(myClassMock).getMessage();
    verifyNew(MyClass.class).withNoArguments();
    assertEquals("Expected and actual did not match", expected, actual);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 39 with MyClass

use of samples.newmocking.MyClass in project powermock by powermock.

the class WhenNewCases method testSimpleMultipleNewPrivate_tooManyTimesExpected.

@Test
public void testSimpleMultipleNewPrivate_tooManyTimesExpected() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock1 = mock(MyClass.class);
    whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock1);
    Whitebox.invokeMethod(tested, "simpleMultipleNewPrivate");
    try {
        verifyNew(MyClass.class, times(4)).withNoArguments();
        fail("Should throw an exception!.");
    } catch (AssertionError e) {
        assertEquals("samples.newmocking.MyClass();\nWanted 4 times but was 3 times.", e.getMessage());
    }
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 40 with MyClass

use of samples.newmocking.MyClass in project powermock by powermock.

the class WhenNewCases method testMultipleNew.

@Test
public void testMultipleNew() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock = mock(MyClass.class);
    whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock);
    when(myClassMock.getMessage()).thenReturn("Hello");
    final String actual = tested.multipleNew();
    verify(myClassMock, times(2)).getMessage();
    verifyNew(MyClass.class, times(2)).withNoArguments();
    assertEquals("HelloHello", actual);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

MyClass (samples.newmocking.MyClass)93 Test (org.junit.Test)91 ExpectNewDemo (samples.expectnew.ExpectNewDemo)75 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)49 PrepareEverythingForTest (org.powermock.core.classloader.annotations.PrepareEverythingForTest)21 StupidNew (samples.newmocking.StupidNew)3 MockitoAssertionError (org.mockito.exceptions.base.MockitoAssertionError)2