Search in sources :

Example 66 with ExpectNewDemo

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

the class WhenNewCases method testNewWithArrayVarArgsWhenFirstArgumentIsNullAndSubseqentArgumentsAreNotNull.

@Test
public void testNewWithArrayVarArgsWhenFirstArgumentIsNullAndSubseqentArgumentsAreNotNull() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    VarArgsConstructorDemo varArgsConstructorDemoMock = mock(VarArgsConstructorDemo.class);
    final byte[] byteArrayOne = null;
    final byte[] byteArrayTwo = new byte[] { 17 };
    whenNew(VarArgsConstructorDemo.class).withArguments(byteArrayOne, byteArrayTwo).thenReturn(varArgsConstructorDemoMock);
    when(varArgsConstructorDemoMock.getByteArrays()).thenReturn(new byte[][] { byteArrayTwo });
    byte[][] varArgs = tested.newVarArgs(byteArrayOne, byteArrayTwo);
    assertEquals(1, varArgs.length);
    assertSame(byteArrayTwo, varArgs[0]);
    verifyNew(VarArgsConstructorDemo.class).withArguments(byteArrayOne, byteArrayTwo);
}
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 67 with ExpectNewDemo

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

the class WhenNewCases method testNewWithRuntimeException.

@Test
public void testNewWithRuntimeException() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    final String expectedFailMessage = "testing";
    whenNew(MyClass.class).withNoArguments().thenThrow(new RuntimeException(expectedFailMessage));
    try {
        tested.throwExceptionWhenInvoction();
        fail("Should throw RuntimeException!");
    } catch (RuntimeException e) {
        assertEquals(expectedFailMessage, e.getMessage());
    }
    verifyNew(MyClass.class).withNoArguments();
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 68 with ExpectNewDemo

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

the class WhenNewCases method testNewWithVarArgsConstructorWhenOneArgumentIsOfASubType.

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

        public String getServiceMessage() {
            return "message";
        }
    };
    whenNew(VarArgsConstructorDemo.class).withArguments(serviceSubTypeInstance, serviceMock).thenReturn(varArgsConstructorDemoMock);
    when(varArgsConstructorDemoMock.getAllServices()).thenReturn(new Service[] { serviceMock });
    Service[] varArgs = tested.newVarArgs(serviceSubTypeInstance, serviceMock);
    assertEquals(1, varArgs.length);
    assertSame(serviceMock, varArgs[0]);
    verifyNew(VarArgsConstructorDemo.class).withArguments(serviceSubTypeInstance, serviceMock);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) VarArgsConstructorDemo(samples.expectnew.VarArgsConstructorDemo) SimpleVarArgsConstructorDemo(samples.expectnew.SimpleVarArgsConstructorDemo) Service(samples.Service) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 69 with ExpectNewDemo

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

the class WhenNewCases method testSimpleMultipleNewPrivate_ok.

@Test
public void testSimpleMultipleNewPrivate_ok() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock1 = mock(MyClass.class);
    whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock1);
    Whitebox.invokeMethod(tested, "simpleMultipleNewPrivate");
    verifyNew(MyClass.class, times(3)).withNoArguments();
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 70 with ExpectNewDemo

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

the class ExpectNewDemoTest method testNewWithRuntimeException.

@Test
public void testNewWithRuntimeException() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    final String expectedFailMessage = "testing";
    expectNew(MyClass.class).andThrow(new RuntimeException(expectedFailMessage));
    replay(MyClass.class);
    try {
        tested.throwExceptionWhenInvoction();
        fail("Should throw RuntimeException!");
    } catch (RuntimeException e) {
        assertEquals(expectedFailMessage, e.getMessage());
    }
    verify(MyClass.class);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) 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