use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class WhenNewCases method testGetMessageWithArgument.
@Test
public void testGetMessageWithArgument() 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("test")).thenReturn("Hello altered World");
String actual = tested.getMessageWithArgument();
verify(myClassMock).getMessage("test");
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 whenNewAnyArgumentsWorksInClassesWithSingleCtor.
@Test
public void whenNewAnyArgumentsWorksInClassesWithSingleCtor() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
MyClass myClassMock = mock(MyClass.class);
whenNew(MyClass.class).withAnyArguments().thenReturn(myClassMock);
when(myClassMock.getMessage()).thenReturn("Hello");
final String actual = tested.multipleNew();
verify(myClassMock, times(2)).getMessage();
verifyNew(MyClass.class, times(2)).withNoArguments();
assertEquals("HelloHello", actual);
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class VerifyNoMoreInteractionsTest method verifyNoMoreInteractionsOnNewInstancesWorksWhenUsingConstructorToExpect.
@Test
public void verifyNoMoreInteractionsOnNewInstancesWorksWhenUsingConstructorToExpect() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
MyClass myClassMock = mock(MyClass.class);
whenNew(constructor(MyClass.class)).withNoArguments().thenReturn(myClassMock);
tested.simpleMultipleNew();
verifyNew(MyClass.class, times(3)).withNoArguments();
verifyNoMoreInteractions(MyClass.class);
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class VerifyNoMoreInteractionsTest method verifyNoMoreInteractionsOnNewInstancesWorks.
@Test
public void verifyNoMoreInteractionsOnNewInstancesWorks() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
MyClass myClassMock = mock(MyClass.class);
whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock);
tested.simpleMultipleNew();
verifyNew(MyClass.class, times(3)).withNoArguments();
verifyNoMoreInteractions(MyClass.class);
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class WhenNewCases method testNewWithArrayVarArgs.
@Test
public void testNewWithArrayVarArgs() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
VarArgsConstructorDemo varArgsConstructorDemoMock = mock(VarArgsConstructorDemo.class);
final byte[] byteArrayOne = new byte[] { 42 };
final byte[] byteArrayTwo = new byte[] { 17 };
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