Search in sources :

Example 71 with ExpectNewDemo

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

the class ExpectNewDemoTest 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 });
    replay(VarArgsConstructorDemo.class, varArgsConstructorDemoMock);
    byte[][] varArgs = tested.newVarArgs(byteArrayOne, byteArrayTwo, byteArrayThree);
    assertEquals(1, varArgs.length);
    assertSame(byteArrayTwo, varArgs[0]);
    verify(VarArgsConstructorDemo.class, varArgsConstructorDemoMock);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) VarArgsConstructorDemo(samples.expectnew.VarArgsConstructorDemo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 72 with ExpectNewDemo

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

the class ExpectNewDemoTest 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 });
    replay(VarArgsConstructorDemo.class, varArgsConstructorDemoMock);
    byte[][] varArgs = tested.newVarArgs(byteArrayOne, byteArrayTwo);
    assertEquals(1, varArgs.length);
    assertSame(byteArrayTwo, varArgs[0]);
    verify(VarArgsConstructorDemo.class, varArgsConstructorDemoMock);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) VarArgsConstructorDemo(samples.expectnew.VarArgsConstructorDemo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 73 with ExpectNewDemo

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

the class ExpectNewDemoTest method testMultipleNew.

@Test
public void testMultipleNew() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock1 = createMock(MyClass.class);
    MyClass myClassMock2 = createMock(MyClass.class);
    expectNew(MyClass.class).andReturn(myClassMock1);
    expectNew(MyClass.class).andReturn(myClassMock2);
    expect(myClassMock1.getMessage()).andReturn("Hello ");
    expect(myClassMock2.getMessage()).andReturn("World");
    replay(myClassMock1, myClassMock2, MyClass.class);
    final String actual = tested.multipleNew();
    verify(myClassMock1, myClassMock2, MyClass.class);
    assertEquals("Hello World", actual);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 74 with ExpectNewDemo

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

the class ExpectNewDemoTest method testSimpleMultipleNew_anyTimes.

@Test
public void testSimpleMultipleNew_anyTimes() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock1 = createMock(MyClass.class);
    expectNew(MyClass.class).andReturn(myClassMock1).anyTimes();
    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 75 with ExpectNewDemo

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

the class ExpectNewDemoTest method testNewWithWrongArgument.

@Test
public void testNewWithWrongArgument() throws Exception {
    final int numberOfTimes = 2;
    final String expected = "used";
    ExpectNewDemo tested = new ExpectNewDemo();
    ExpectNewServiceUser expectNewServiceImplMock = createMock(ExpectNewServiceUser.class);
    Service serviceMock = createMock(Service.class);
    expectNew(ExpectNewServiceUser.class, serviceMock, numberOfTimes).andReturn(expectNewServiceImplMock);
    expect(expectNewServiceImplMock.useService()).andReturn(expected);
    replay(expectNewServiceImplMock, serviceMock, ExpectNewServiceUser.class);
    try {
        assertEquals(expected, tested.newWithWrongArguments(serviceMock, numberOfTimes));
        verify(expectNewServiceImplMock, serviceMock, ExpectNewServiceUser.class);
        fail("Should throw AssertionError!");
    } catch (AssertionError e) {
        assertEquals("\n  Unexpected constructor call samples.expectnew.ExpectNewServiceUser(EasyMock for interface samples.Service, 4):" + "\n    samples.expectnew.ExpectNewServiceUser(EasyMock for interface samples.Service, 2): expected: 1, actual: 0", e.getMessage());
    }
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) ExpectNewServiceUser(samples.expectnew.ExpectNewServiceUser) Service(samples.Service) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

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