Search in sources :

Example 46 with MyClass

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

the class VerifyNoMoreInteractionsTest method verifyNoMoreInteractionsOnNewInstancesWorks.

@Test
public void verifyNoMoreInteractionsOnNewInstancesWorks() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock = mock(MyClass.class);
    whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock);
    tested.simpleMultipleNew();
    verifyNew(MyClass.class, times(3)).withNoArguments();
    verifyNoMoreInteractions(MyClass.class);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 47 with MyClass

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

the class WhenNewCases method testSimpleMultipleNewPrivate_ok.

@Test
public void testSimpleMultipleNewPrivate_ok() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock1 = mock(MyClass.class);
    whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock1);
    Whitebox.invokeMethod(tested, "simpleMultipleNewPrivate");
    verifyNew(MyClass.class, times(3)).withNoArguments();
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 48 with MyClass

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

the class ExpectNewDemoTest method testMultipleNew.

@Test
public void testMultipleNew() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock1 = createMock(MyClass.class);
    MyClass myClassMock2 = createMock(MyClass.class);
    expectNew(MyClass.class).andReturn(myClassMock1);
    expectNew(MyClass.class).andReturn(myClassMock2);
    expect(myClassMock1.getMessage()).andReturn("Hello ");
    expect(myClassMock2.getMessage()).andReturn("World");
    replay(myClassMock1, myClassMock2, MyClass.class);
    final String actual = tested.multipleNew();
    verify(myClassMock1, myClassMock2, MyClass.class);
    assertEquals("Hello World", actual);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 49 with MyClass

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

the class ExpectNewDemoTest method testSimpleMultipleNew_anyTimes.

@Test
public void testSimpleMultipleNew_anyTimes() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock1 = createMock(MyClass.class);
    expectNew(MyClass.class).andReturn(myClassMock1).anyTimes();
    replay(myClassMock1, MyClass.class);
    tested.simpleMultipleNew();
    verify(myClassMock1, MyClass.class);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 50 with MyClass

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

the class ExpectNewDemoTest method testSimpleMultipleNew_withRange_upperBoundLessThanLowerBound.

@Test
public void testSimpleMultipleNew_withRange_upperBoundLessThanLowerBound() throws Exception {
    MyClass myClassMock1 = createMock(MyClass.class);
    try {
        expectNew(MyClass.class).andReturn(myClassMock1).times(10, 2);
        fail("Should throw IllegalArgumentException.");
    } catch (IllegalArgumentException e) {
        assertTrue(e.getMessage().contains("<="));
    }
}
Also used : 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