Search in sources :

Example 11 with MyClass

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

the class ExpectNewCases method testSimpleSingleNew_withOnce.

@Test
public void testSimpleSingleNew_withOnce() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock1 = createMock(MyClass.class);
    expectNew(MyClass.class).andReturn(myClassMock1).once();
    replay(myClassMock1, MyClass.class);
    tested.simpleSingleNew();
    verify(myClassMock1, MyClass.class);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) Test(org.junit.Test)

Example 12 with MyClass

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

the class ExpectNewCases method testSimpleMultipleNewPrivate_ok.

@Test
public void testSimpleMultipleNewPrivate_ok() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock1 = createMock(MyClass.class);
    expectNew(MyClass.class).andReturn(myClassMock1).times(3);
    replay(myClassMock1, MyClass.class);
    Whitebox.invokeMethod(tested, "simpleMultipleNewPrivate");
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) Test(org.junit.Test)

Example 13 with MyClass

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

the class ExpectNewCases method testSimpleMultipleNewPrivate_tooManyTimesExpected.

@Test
public void testSimpleMultipleNewPrivate_tooManyTimesExpected() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock1 = createMock(MyClass.class);
    expectNew(MyClass.class).andReturn(myClassMock1).times(4);
    replay(myClassMock1, MyClass.class);
    try {
        Whitebox.invokeMethod(tested, "simpleMultipleNewPrivate");
        verify(myClassMock1, MyClass.class);
        fail("Should throw an exception!.");
    } catch (AssertionError e) {
        assertEquals("\n  Expectation failure on verify:" + "\n    samples.newmocking.MyClass(): expected: 4, actual: 3", e.getMessage());
    }
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) Test(org.junit.Test)

Example 14 with MyClass

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

the class ExpectNewCases method testSimpleMultipleNew_withRange_lowerBoundLessThan0.

@Test
public void testSimpleMultipleNew_withRange_lowerBoundLessThan0() throws Exception {
    MyClass myClassMock1 = createMock(MyClass.class);
    try {
        expectNew(MyClass.class).andReturn(myClassMock1).times(-20, 2);
        fail("Should throw IllegalArgumentException.");
    } catch (IllegalArgumentException e) {
        assertEquals("minimum must be >= 0", e.getMessage());
    }
}
Also used : MyClass(samples.newmocking.MyClass) Test(org.junit.Test)

Example 15 with MyClass

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

the class ExpectNewCases method testSimpleMultipleNew_withRange_upperBoundLessThan0.

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