Search in sources :

Example 31 with MyClass

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

the class ExpectNewDemoUsingThePrepareEverythingAnnotationTest 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);
    replayAll();
    tested.invokeVoidMethod();
    verifyAll();
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) Test(org.junit.Test) PrepareEverythingForTest(org.powermock.core.classloader.annotations.PrepareEverythingForTest)

Example 32 with MyClass

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

the class ExpectNewDemoUsingThePrepareEverythingAnnotationTest 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);
    replayAll();
    tested.simpleMultipleNew();
    try {
        verifyAll();
        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) Test(org.junit.Test) PrepareEverythingForTest(org.powermock.core.classloader.annotations.PrepareEverythingForTest)

Example 33 with MyClass

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

the class ExpectNewDemoUsingThePrepareEverythingAnnotationTest 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);
    replayAll();
    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) PrepareEverythingForTest(org.powermock.core.classloader.annotations.PrepareEverythingForTest)

Example 34 with MyClass

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

the class ExpectNewDemoUsingThePrepareEverythingAnnotationTest 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) PrepareEverythingForTest(org.powermock.core.classloader.annotations.PrepareEverythingForTest)

Example 35 with MyClass

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

the class VerifyNoMoreInteractionsTest method verifyNoMoreInteractionsOnNewInstancesThrowsAssertionErrorWhenMoreInteractionsTookPlace.

@Test
public void verifyNoMoreInteractionsOnNewInstancesThrowsAssertionErrorWhenMoreInteractionsTookPlace() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock = mock(MyClass.class);
    whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock);
    tested.simpleMultipleNew();
    try {
        verifyNoMoreInteractions(MyClass.class);
        fail("Should throw exception!");
    } catch (MockitoAssertionError e) {
        assertTrue(e.getMessage().startsWith("\nNo interactions wanted here:\n-> at samples.powermockito.junit4.verifynomoreinteractions.VerifyNoMoreInteractionsTest.verifyNoMoreInteractionsOnNewInstancesThrowsAssertionErrorWhenMoreInteractionsTookPlace(VerifyNoMoreInteractionsTest.java:"));
    }
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) MockitoAssertionError(org.mockito.exceptions.base.MockitoAssertionError) 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