Search in sources :

Example 41 with MyClass

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

the class ExpectNewDemoTest method testSimpleMultipleNew_withRange_notWithinRange.

@Test
public void testSimpleMultipleNew_withRange_notWithinRange() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock1 = createMock(MyClass.class);
    expectNew(MyClass.class).andReturn(myClassMock1).times(5, 7);
    replay(myClassMock1, MyClass.class);
    tested.simpleMultipleNew();
    try {
        verify(myClassMock1, MyClass.class);
        fail("Should throw AssertionError.");
    } catch (AssertionError e) {
        assertEquals("\n  Expectation failure on verify:" + "\n    samples.newmocking.MyClass(): expected: between 5 and 7, actual: 3", e.getMessage());
    }
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 42 with MyClass

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

the class ExpectNewDemoTest method testSimpleMultipleNewPrivate_tooFewTimesExpected.

/**
     * Verifies that the issue
     * http://code.google.com/p/powermock/issues/detail?id=10 is solved.
     */
@Test
public void testSimpleMultipleNewPrivate_tooFewTimesExpected() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock1 = createMock(MyClass.class);
    expectNew(MyClass.class).andReturn(myClassMock1).times(2);
    replay(myClassMock1, MyClass.class);
    try {
        Whitebox.invokeMethod(tested, "simpleMultipleNewPrivate");
        fail("Should throw AssertionError.");
    } catch (AssertionError e) {
        assertEquals("\n  Unexpected constructor call samples.newmocking.MyClass():" + "\n    samples.newmocking.MyClass(): expected: 2, actual: 3", e.getMessage());
    }
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 43 with MyClass

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

the class ExpectNewDemoTest method testInvokeVoidMethod.

@Test
public void testInvokeVoidMethod() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock = createMock(MyClass.class);
    expectNew(MyClass.class).andReturn(myClassMock);
    myClassMock.voidMethod();
    expectLastCall().times(1);
    replay(myClassMock, MyClass.class);
    tested.invokeVoidMethod();
    verify(myClassMock, MyClass.class);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 44 with MyClass

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

the class ExpectNewDemoTest method testSimpleMultipleNew_withRange_OK.

@Test
public void testSimpleMultipleNew_withRange_OK() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock1 = createMock(MyClass.class);
    expectNew(MyClass.class).andReturn(myClassMock1).times(1, 5);
    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 45 with MyClass

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

the class VerifyNoMoreInteractionsTest method verifyNoMoreInteractionsOnNewInstancesWorksWhenUsingConstructorToExpect.

@Test
public void verifyNoMoreInteractionsOnNewInstancesWorksWhenUsingConstructorToExpect() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock = mock(MyClass.class);
    whenNew(constructor(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)

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