use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class WhenNewCases method testGetMessage.
@Test
public void testGetMessage() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
MyClass myClassMock = mock(MyClass.class);
whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock);
String expected = "Hello altered World";
when(myClassMock.getMessage()).thenReturn("Hello altered World");
String actual = tested.getMessage();
verify(myClassMock).getMessage();
verifyNew(MyClass.class).withNoArguments();
assertEquals("Expected and actual did not match", expected, actual);
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class WhenNewCases method testNewWithCheckedException.
@Test
public void testNewWithCheckedException() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
final String expectedFailMessage = "testing checked exception";
whenNew(MyClass.class).withNoArguments().thenThrow(new IOException(expectedFailMessage));
try {
tested.throwExceptionAndWrapInRunTimeWhenInvoction();
fail("Should throw a checked Exception!");
} catch (RuntimeException e) {
assertTrue(e.getCause() instanceof IOException);
assertEquals(expectedFailMessage, e.getMessage());
}
verifyNew(MyClass.class).withNoArguments();
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class WhenNewCases method testNewWithArguments.
@Test
public void testNewWithArguments() throws Exception {
final int numberOfTimes = 2;
final String expected = "used";
ExpectNewDemo tested = new ExpectNewDemo();
ExpectNewServiceUser expectNewServiceImplMock = mock(ExpectNewServiceUser.class);
Service serviceMock = mock(Service.class);
whenNew(ExpectNewServiceUser.class).withArguments(serviceMock, numberOfTimes).thenReturn(expectNewServiceImplMock);
when(expectNewServiceImplMock.useService()).thenReturn(expected);
assertEquals(expected, tested.newWithArguments(serviceMock, numberOfTimes));
verifyNew(ExpectNewServiceUser.class).withArguments(serviceMock, numberOfTimes);
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class WhenNewCases method testNewUsingConstructorWithArguments.
@Test
public void testNewUsingConstructorWithArguments() throws Exception {
final int numberOfTimes = 2;
final String expected = "used";
ExpectNewDemo tested = new ExpectNewDemo();
ExpectNewServiceUser expectNewServiceImplMock = mock(ExpectNewServiceUser.class);
Service serviceMock = mock(Service.class);
whenNew(constructor(ExpectNewServiceUser.class)).withArguments(serviceMock, numberOfTimes).thenReturn(expectNewServiceImplMock);
when(expectNewServiceImplMock.useService()).thenReturn(expected);
assertEquals(expected, tested.newWithArguments(serviceMock, numberOfTimes));
verifyNew(ExpectNewServiceUser.class).withArguments(serviceMock, numberOfTimes);
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class WhenNewCases method testNewWithArrayVarArgsWhenFirstArgumentIsNotNullButSubseqentArgumentsAreNull.
@Test
public void testNewWithArrayVarArgsWhenFirstArgumentIsNotNullButSubseqentArgumentsAreNull() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
VarArgsConstructorDemo varArgsConstructorDemoMock = mock(VarArgsConstructorDemo.class);
final byte[] byteArrayOne = new byte[] { 42 };
final byte[] byteArrayTwo = null;
whenNew(VarArgsConstructorDemo.class).withArguments(byteArrayOne, byteArrayTwo).thenReturn(varArgsConstructorDemoMock);
when(varArgsConstructorDemoMock.getByteArrays()).thenReturn(new byte[][] { byteArrayOne });
byte[][] varArgs = tested.newVarArgs(byteArrayOne, byteArrayTwo);
assertEquals(1, varArgs.length);
assertSame(byteArrayOne, varArgs[0]);
verifyNew(VarArgsConstructorDemo.class).withArguments(byteArrayOne, byteArrayTwo);
}
Aggregations