Search in sources :

Example 6 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 7 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 8 with MyClass

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

the class ExpectNewCases 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) Test(org.junit.Test)

Example 9 with MyClass

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

the class ExpectNewCases method testExpectNewButNoNewCallWasMade.

@Test
public void testExpectNewButNoNewCallWasMade() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock1 = createMock(MyClass.class);
    expectNew(MyClass.class).andReturn(myClassMock1).once();
    replay(myClassMock1, MyClass.class);
    try {
        tested.makeDate();
        verify(myClassMock1, MyClass.class);
        fail("Should throw AssertionError!");
    } catch (AssertionError e) {
        assertTrue(e.getMessage().contains(MyClass.class.getName() + "(): expected: 1, actual: 0"));
    }
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) Test(org.junit.Test)

Example 10 with MyClass

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

the class ExpectNewCases method testSimpleMultipleNew_tooFewTimesExpected.

@Test
public void testSimpleMultipleNew_tooFewTimesExpected() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock1 = createMock(MyClass.class);
    expectNew(MyClass.class).andReturn(myClassMock1).times(2);
    replay(myClassMock1, MyClass.class);
    try {
        tested.simpleMultipleNew();
        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) 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