use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class WhenNewCases method testExpectNewButNoNewCallWasMade.
@Test
public void testExpectNewButNoNewCallWasMade() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
MyClass myClassMock1 = mock(MyClass.class);
whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock1);
tested.makeDate();
try {
verifyNew(MyClass.class).withNoArguments();
fail("Should throw AssertionError!");
} catch (AssertionError e) {
assertEquals("Wanted but not invoked samples.newmocking.MyClass();\nActually, there were zero interactions with this mock.", e.getMessage());
}
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class WhenNewCases method testNewWithArrayVarArgsWhenAllArgumentsAreNullAndOverloadedVarArgsConstructors.
@Test
public void testNewWithArrayVarArgsWhenAllArgumentsAreNullAndOverloadedVarArgsConstructors() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
VarArgsConstructorDemo varArgsConstructorDemoMock = mock(VarArgsConstructorDemo.class);
final byte[] byteArrayOne = null;
final byte[] byteArrayTwo = null;
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 testSimpleSingleNew_withAtLeastOnce.
@Test
public void testSimpleSingleNew_withAtLeastOnce() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
MyClass myClassMock1 = mock(MyClass.class);
whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock1);
tested.simpleSingleNew();
verifyNew(MyClass.class, atLeastOnce()).withNoArguments();
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class WhenNewCases method testAlternativeFlow.
//
@Test
public void testAlternativeFlow() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
whenNew(DataInputStream.class).withArguments(null).thenThrow(new RuntimeException("error"));
InputStream stream = tested.alternativePath();
verifyNew(DataInputStream.class).withArguments(null);
assertNotNull("The returned inputstream should not be null.", stream);
assertTrue("The returned inputstream should be an instance of ByteArrayInputStream.", stream instanceof ByteArrayInputStream);
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class WhenNewCases method testNewWithVarArgs.
@Test
public void testNewWithVarArgs() throws Exception {
final String firstString = "hello";
final String secondString = "world";
ExpectNewDemo tested = new ExpectNewDemo();
VarArgsConstructorDemo varArgsConstructorDemoMock = mock(VarArgsConstructorDemo.class);
whenNew(VarArgsConstructorDemo.class).withArguments(firstString, secondString).thenReturn(varArgsConstructorDemoMock);
when(varArgsConstructorDemoMock.getAllMessages()).thenReturn(new String[] { firstString, secondString });
String[] varArgs = tested.newVarArgs(firstString, secondString);
assertEquals(2, varArgs.length);
assertEquals(firstString, varArgs[0]);
assertEquals(secondString, varArgs[1]);
verifyNew(VarArgsConstructorDemo.class).withArguments(firstString, secondString);
}
Aggregations