use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class ExpectNewCases 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 ExpectNewCases 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));
replay(MyClass.class);
try {
tested.throwExceptionAndWrapInRunTimeWhenInvoction();
fail("Should throw a checked Exception!");
} catch (RuntimeException e) {
assertTrue(e.getCause() instanceof IOException);
assertEquals(expectedFailMessage, e.getMessage());
}
verify(MyClass.class);
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class ExpectNewCases method testNewWithArrayVarArgsWhenFirstArgumentIsNotNullButSubseqentArgumentsAreNull.
@Test
public void testNewWithArrayVarArgsWhenFirstArgumentIsNotNullButSubseqentArgumentsAreNull() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
VarArgsConstructorDemo varArgsConstructorDemoMock = createMock(VarArgsConstructorDemo.class);
final byte[] byteArrayOne = new byte[] { 42 };
final byte[] byteArrayTwo = null;
expectNew(VarArgsConstructorDemo.class, byteArrayOne, byteArrayTwo).andReturn(varArgsConstructorDemoMock);
expect(varArgsConstructorDemoMock.getByteArrays()).andReturn(new byte[][] { byteArrayOne });
replay(VarArgsConstructorDemo.class, varArgsConstructorDemoMock);
byte[][] varArgs = tested.newVarArgs(byteArrayOne, byteArrayTwo);
assertEquals(1, varArgs.length);
assertSame(byteArrayOne, varArgs[0]);
verify(VarArgsConstructorDemo.class, varArgsConstructorDemoMock);
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class ExpectNewCases method testNewWithVarArgsConstructorWhenOneArgumentIsOfASubType.
@Test
public void testNewWithVarArgsConstructorWhenOneArgumentIsOfASubType() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
Service serviceMock = createMock(Service.class);
VarArgsConstructorDemo varArgsConstructorDemoMock = createMock(VarArgsConstructorDemo.class);
final Service serviceSubTypeInstance = new Service() {
public String getServiceMessage() {
return "message";
}
};
expectNew(VarArgsConstructorDemo.class, serviceSubTypeInstance, serviceMock).andReturn(varArgsConstructorDemoMock);
expect(varArgsConstructorDemoMock.getAllServices()).andReturn(new Service[] { serviceMock });
replay(serviceMock, VarArgsConstructorDemo.class, varArgsConstructorDemoMock);
Service[] varArgs = tested.newVarArgs(serviceSubTypeInstance, serviceMock);
assertEquals(1, varArgs.length);
assertSame(serviceMock, varArgs[0]);
verify(serviceMock, VarArgsConstructorDemo.class, varArgsConstructorDemoMock);
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class ExpectNewCases method testSimpleSingleNew_withOnce.
@Test
public void testSimpleSingleNew_withOnce() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
MyClass myClassMock1 = createMock(MyClass.class);
expectNew(MyClass.class).andReturn(myClassMock1).once();
replay(myClassMock1, MyClass.class);
tested.simpleSingleNew();
verify(myClassMock1, MyClass.class);
}
Aggregations