Search in sources :

Example 6 with VarArgsConstructorDemo

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

the class WhenNewCases method whenNewSupportsVarArgsAsSecondParameter.

@Test
public void whenNewSupportsVarArgsAsSecondParameter() throws Exception {
    final int one = 1;
    final int two = 2;
    final float myFloat = 3.0f;
    ExpectNewDemo tested = new ExpectNewDemo();
    VarArgsConstructorDemo varArgsConstructorDemoMock = mock(VarArgsConstructorDemo.class);
    whenNew(VarArgsConstructorDemo.class).withArguments(myFloat, one, two).thenReturn(varArgsConstructorDemoMock);
    when(varArgsConstructorDemoMock.getInts()).thenReturn(new int[] { one, two });
    int[] varArgs = tested.newVarArgs(myFloat, one, two);
    assertEquals(2, varArgs.length);
    assertEquals(one, varArgs[0]);
    assertEquals(two, varArgs[1]);
    verifyNew(VarArgsConstructorDemo.class).withArguments(myFloat, one, two);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) VarArgsConstructorDemo(samples.expectnew.VarArgsConstructorDemo) SimpleVarArgsConstructorDemo(samples.expectnew.SimpleVarArgsConstructorDemo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with VarArgsConstructorDemo

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

the class WhenNewCases method testNewWithArrayVarArgsWhenFirstArgumentIsNullSecondArgumentIsNotNullAndThirdArgumentIsNull.

@Test
public void testNewWithArrayVarArgsWhenFirstArgumentIsNullSecondArgumentIsNotNullAndThirdArgumentIsNull() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    VarArgsConstructorDemo varArgsConstructorDemoMock = mock(VarArgsConstructorDemo.class);
    final byte[] byteArrayOne = null;
    final byte[] byteArrayTwo = new byte[] { 42 };
    final byte[] byteArrayThree = null;
    whenNew(VarArgsConstructorDemo.class).withArguments(byteArrayOne, byteArrayTwo, byteArrayThree).thenReturn(varArgsConstructorDemoMock);
    when(varArgsConstructorDemoMock.getByteArrays()).thenReturn(new byte[][] { byteArrayTwo });
    byte[][] varArgs = tested.newVarArgs(byteArrayOne, byteArrayTwo, byteArrayThree);
    assertEquals(1, varArgs.length);
    assertSame(byteArrayTwo, varArgs[0]);
    verifyNew(VarArgsConstructorDemo.class).withArguments(byteArrayOne, byteArrayTwo, byteArrayThree);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) VarArgsConstructorDemo(samples.expectnew.VarArgsConstructorDemo) SimpleVarArgsConstructorDemo(samples.expectnew.SimpleVarArgsConstructorDemo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with VarArgsConstructorDemo

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

the class ExpectNewCases method testNewWithArrayVarArgsWhenFirstArgumentIsNotNullButSubseqentArgumentsAreNull.

@Test
public void testNewWithArrayVarArgsWhenFirstArgumentIsNotNullButSubseqentArgumentsAreNull() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    VarArgsConstructorDemo varArgsConstructorDemoMock = createMock(VarArgsConstructorDemo.class);
    final byte[] byteArrayOne = new byte[] { 42 };
    final byte[] byteArrayTwo = null;
    expectNew(VarArgsConstructorDemo.class, byteArrayOne, byteArrayTwo).andReturn(varArgsConstructorDemoMock);
    expect(varArgsConstructorDemoMock.getByteArrays()).andReturn(new byte[][] { byteArrayOne });
    replay(VarArgsConstructorDemo.class, varArgsConstructorDemoMock);
    byte[][] varArgs = tested.newVarArgs(byteArrayOne, byteArrayTwo);
    assertEquals(1, varArgs.length);
    assertSame(byteArrayOne, varArgs[0]);
    verify(VarArgsConstructorDemo.class, varArgsConstructorDemoMock);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) VarArgsConstructorDemo(samples.expectnew.VarArgsConstructorDemo) Test(org.junit.Test)

Example 9 with VarArgsConstructorDemo

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

the class ExpectNewCases method testNewWithVarArgsConstructorWhenOneArgumentIsOfASubType.

@Test
public void testNewWithVarArgsConstructorWhenOneArgumentIsOfASubType() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    Service serviceMock = createMock(Service.class);
    VarArgsConstructorDemo varArgsConstructorDemoMock = createMock(VarArgsConstructorDemo.class);
    final Service serviceSubTypeInstance = new Service() {

        public String getServiceMessage() {
            return "message";
        }
    };
    expectNew(VarArgsConstructorDemo.class, serviceSubTypeInstance, serviceMock).andReturn(varArgsConstructorDemoMock);
    expect(varArgsConstructorDemoMock.getAllServices()).andReturn(new Service[] { serviceMock });
    replay(serviceMock, VarArgsConstructorDemo.class, varArgsConstructorDemoMock);
    Service[] varArgs = tested.newVarArgs(serviceSubTypeInstance, serviceMock);
    assertEquals(1, varArgs.length);
    assertSame(serviceMock, varArgs[0]);
    verify(serviceMock, VarArgsConstructorDemo.class, varArgsConstructorDemoMock);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) VarArgsConstructorDemo(samples.expectnew.VarArgsConstructorDemo) Service(samples.Service) Test(org.junit.Test)

Example 10 with VarArgsConstructorDemo

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

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

Aggregations

Test (org.junit.Test)33 ExpectNewDemo (samples.expectnew.ExpectNewDemo)33 VarArgsConstructorDemo (samples.expectnew.VarArgsConstructorDemo)33 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)17 SimpleVarArgsConstructorDemo (samples.expectnew.SimpleVarArgsConstructorDemo)9 PrepareEverythingForTest (org.powermock.core.classloader.annotations.PrepareEverythingForTest)8 Service (samples.Service)4