use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class WhenNewCases method testNewWithArrayVarArgsWhenAllArgumentsAreNull.
@Test
public void testNewWithArrayVarArgsWhenAllArgumentsAreNull() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
SimpleVarArgsConstructorDemo varArgsConstructorDemoMock = mock(SimpleVarArgsConstructorDemo.class);
final byte[] byteArrayOne = null;
final byte[] byteArrayTwo = null;
whenNew(SimpleVarArgsConstructorDemo.class).withArguments(byteArrayOne, byteArrayTwo).thenReturn(varArgsConstructorDemoMock);
when(varArgsConstructorDemoMock.getByteArrays()).thenReturn(new byte[][] { byteArrayTwo });
byte[][] varArgs = tested.newSimpleVarArgs(byteArrayOne, byteArrayTwo);
assertEquals(1, varArgs.length);
assertSame(byteArrayTwo, varArgs[0]);
verifyNew(SimpleVarArgsConstructorDemo.class).withArguments(byteArrayOne, byteArrayTwo);
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class WhenNewCases method testNewWithConstructorUsingParameterTypesAndArguments.
@Test
public void testNewWithConstructorUsingParameterTypesAndArguments() 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, Service.class, int.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 testSimpleMultipleNew_tooFewTimesExpected.
@Test
public void testSimpleMultipleNew_tooFewTimesExpected() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
MyClass myClassMock1 = mock(MyClass.class);
whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock1);
tested.simpleMultipleNew();
try {
verifyNew(MyClass.class, times(1)).withNoArguments();
fail("Should throw AssertionError.");
} catch (AssertionError e) {
assertEquals("samples.newmocking.MyClass();\nWanted 1 time but was 3 times.", e.getMessage());
}
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class ExpectNewCases method testNewWithArguments.
@Test
public void testNewWithArguments() 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);
assertEquals(expected, tested.newWithArguments(serviceMock, numberOfTimes));
verify(expectNewServiceImplMock, serviceMock, ExpectNewServiceUser.class);
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class ReplayAllForExpectNewTest method testNewWithCheckedException.
@Test
public void testNewWithCheckedException() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
final String expectedFailMessage = "testing checked exception";
expectNew(MyClass.class).andThrow(new IOException(expectedFailMessage));
replayAll();
try {
tested.throwExceptionAndWrapInRunTimeWhenInvoction();
fail("Should throw a checked Exception!");
} catch (RuntimeException e) {
assertTrue(e.getCause() instanceof IOException);
assertEquals(expectedFailMessage, e.getMessage());
}
verifyAll();
}
Aggregations