use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class ExpectNewDemoTest 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 ExpectNewDemoTest method testSimpleMultipleNew_tooManyTimesExpected.
@Test
public void testSimpleMultipleNew_tooManyTimesExpected() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
MyClass myClassMock1 = createMock(MyClass.class);
expectNew(MyClass.class).andReturn(myClassMock1).times(4);
replay(myClassMock1, MyClass.class);
tested.simpleMultipleNew();
try {
verify(myClassMock1, MyClass.class);
fail("Should throw AssertionError.");
} catch (AssertionError e) {
assertEquals("\n Expectation failure on verify:" + "\n samples.newmocking.MyClass(): expected: 4, actual: 3", e.getMessage());
}
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class ExpectNewDemoTest method testNewWithArrayVarArgsWhenFirstArgumentIsNullAndSubseqentArgumentsAreNotNull.
@Test
public void testNewWithArrayVarArgsWhenFirstArgumentIsNullAndSubseqentArgumentsAreNotNull() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
VarArgsConstructorDemo varArgsConstructorDemoMock = createMock(VarArgsConstructorDemo.class);
final byte[] byteArrayOne = null;
final byte[] byteArrayTwo = new byte[] { 17 };
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);
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class ExpectNewDemoTest method testSimpleMultipleNew_tooFewTimesExpected.
@Test
public void testSimpleMultipleNew_tooFewTimesExpected() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
MyClass myClassMock1 = createMock(MyClass.class);
expectNew(MyClass.class).andReturn(myClassMock1).times(2);
replay(myClassMock1, MyClass.class);
try {
tested.simpleMultipleNew();
fail("Should throw AssertionError.");
} catch (AssertionError e) {
assertEquals("\n Unexpected constructor call samples.newmocking.MyClass():" + "\n samples.newmocking.MyClass(): expected: 2, actual: 3", e.getMessage());
}
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class ExpectNewDemoTest method testAlternativeFlow.
@Test
public void testAlternativeFlow() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
expectNew(DataInputStream.class, new Object[] { null }).andThrow(new RuntimeException("error"));
replay(ExpectNewDemo.class, DataInputStream.class);
InputStream stream = tested.alternativePath();
verify(ExpectNewDemo.class, DataInputStream.class);
assertNotNull("The returned inputstream should not be null.", stream);
assertTrue("The returned inputstream should be an instance of ByteArrayInputStream.", stream instanceof ByteArrayInputStream);
}
Aggregations