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);
}
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();
}
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);
}
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();
}
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);
}
Aggregations