Search in sources :

Example 1 with ExpectNewDemo

use of samples.expectnew.ExpectNewDemo in project powermock by powermock.

the class ExpectNewDemoUsingThePrepareEverythingAnnotationTest method testNewWithArrayVarArgsWhenAllArgumentsAreNull.

@Test
public void testNewWithArrayVarArgsWhenAllArgumentsAreNull() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    VarArgsConstructorDemo varArgsConstructorDemoMock = createMock(VarArgsConstructorDemo.class);
    final byte[] byteArrayOne = null;
    final byte[] byteArrayTwo = null;
    expectNew(VarArgsConstructorDemo.class, byteArrayOne, byteArrayTwo).andReturn(varArgsConstructorDemoMock);
    expect(varArgsConstructorDemoMock.getByteArrays()).andReturn(new byte[][] { byteArrayTwo });
    replayAll();
    byte[][] varArgs = tested.newVarArgs(byteArrayOne, byteArrayTwo);
    assertEquals(1, varArgs.length);
    assertSame(byteArrayTwo, varArgs[0]);
    verifyAll();
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) VarArgsConstructorDemo(samples.expectnew.VarArgsConstructorDemo) Test(org.junit.Test) PrepareEverythingForTest(org.powermock.core.classloader.annotations.PrepareEverythingForTest)

Example 2 with ExpectNewDemo

use of samples.expectnew.ExpectNewDemo in project powermock by powermock.

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

Example 3 with ExpectNewDemo

use of samples.expectnew.ExpectNewDemo in project powermock by powermock.

the class ExpectNewDemoUsingThePrepareEverythingAnnotationTest method testNewWithArrayVarArgsWhenFirstArgumentIsNullSecondArgumentIsNotNullAndThirdArgumentIsNull.

@Test
public void testNewWithArrayVarArgsWhenFirstArgumentIsNullSecondArgumentIsNotNullAndThirdArgumentIsNull() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    VarArgsConstructorDemo varArgsConstructorDemoMock = createMock(VarArgsConstructorDemo.class);
    final byte[] byteArrayOne = null;
    final byte[] byteArrayTwo = new byte[] { 42 };
    final byte[] byteArrayThree = null;
    expectNew(VarArgsConstructorDemo.class, byteArrayOne, byteArrayTwo, byteArrayThree).andReturn(varArgsConstructorDemoMock);
    expect(varArgsConstructorDemoMock.getByteArrays()).andReturn(new byte[][] { byteArrayTwo });
    replayAll();
    byte[][] varArgs = tested.newVarArgs(byteArrayOne, byteArrayTwo, byteArrayThree);
    assertEquals(1, varArgs.length);
    assertSame(byteArrayTwo, varArgs[0]);
    verifyAll();
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) VarArgsConstructorDemo(samples.expectnew.VarArgsConstructorDemo) Test(org.junit.Test) PrepareEverythingForTest(org.powermock.core.classloader.annotations.PrepareEverythingForTest)

Example 4 with ExpectNewDemo

use of samples.expectnew.ExpectNewDemo in project powermock by powermock.

the class MultiReplayVerifyTest method replyFollowedByReplayAllIsAllowed.

@Test
public void replyFollowedByReplayAllIsAllowed() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock = createMock(MyClass.class);
    expectNew(MyClass.class).andReturn(myClassMock);
    String expected = "Hello altered World";
    expect(myClassMock.getMessage()).andReturn("Hello altered World");
    replay(MyClass.class);
    replayAll();
    String actual = tested.getMessage();
    verifyAll();
    assertEquals("Expected and actual did not match", expected, actual);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with ExpectNewDemo

use of samples.expectnew.ExpectNewDemo in project powermock by powermock.

the class ExpectNewDemoUsingThePrepareEverythingAnnotationTest 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);
    replayAll();
    try {
        Whitebox.invokeMethod(tested, "simpleMultipleNewPrivate");
        verifyAll();
        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) PrepareEverythingForTest(org.powermock.core.classloader.annotations.PrepareEverythingForTest)

Aggregations

Test (org.junit.Test)143 ExpectNewDemo (samples.expectnew.ExpectNewDemo)143 MyClass (samples.newmocking.MyClass)86 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)82 VarArgsConstructorDemo (samples.expectnew.VarArgsConstructorDemo)33 PrepareEverythingForTest (org.powermock.core.classloader.annotations.PrepareEverythingForTest)30 Service (samples.Service)17 ExpectNewServiceUser (samples.expectnew.ExpectNewServiceUser)13 SimpleVarArgsConstructorDemo (samples.expectnew.SimpleVarArgsConstructorDemo)10 IOException (java.io.IOException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 DataInputStream (java.io.DataInputStream)4 InputStream (java.io.InputStream)4 CreationException (samples.expectnew.CreationException)2 ITarget (samples.expectnew.ITarget)2 Target (samples.expectnew.Target)2 Date (java.util.Date)1 MockitoAssertionError (org.mockito.exceptions.base.MockitoAssertionError)1