use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class WhenNewCases method testSimpleSingleNew_withOnce.
@Test
public void testSimpleSingleNew_withOnce() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
MyClass myClassMock1 = mock(MyClass.class);
whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock1);
tested.simpleSingleNew();
verifyNew(MyClass.class).withNoArguments();
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class WhenNewCases method testNewWithWrongArgument.
@Test(expected = NullPointerException.class)
public void testNewWithWrongArgument() 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(ExpectNewServiceUser.class).withArguments(serviceMock, numberOfTimes).thenReturn(expectNewServiceImplMock);
when(expectNewServiceImplMock.useService()).thenReturn(expected);
assertEquals(expected, tested.newWithWrongArguments(serviceMock, numberOfTimes));
verifyNew(ExpectNewServiceUser.class).withArguments(serviceMock, numberOfTimes);
/*
* Should throw NPE because the default behavior of Mockito when a
* something isn't expected is to return a default value. In this case
* whenConstructionOf
* (ExpectNewServiceUser.class).withArguments(serviceMock,
* numberOfTimes) is the wrong expectation and thus null is returned
* from the substitute mock which is the correct behavior.
*/
fail("Should throw NPE!");
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class WhenNewCases method testSimpleMultipleNew.
@Test
public void testSimpleMultipleNew() 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();
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class WhenNewCases method testSimpleMultipleNew_withAtLeastOnce.
@Test
public void testSimpleMultipleNew_withAtLeastOnce() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
MyClass myClassMock1 = mock(MyClass.class);
whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock1);
tested.simpleMultipleNew();
verifyNew(MyClass.class, atLeastOnce()).withNoArguments();
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class WhenNewCases method canDefineSeveralMockResultForNew.
@Test
public void canDefineSeveralMockResultForNew() throws Exception {
final Target expectedTarget = new Target(UNKNOWN_TARGET_NAME, UNKNOWN_TARGET_ID);
whenNew(Target.class).withArguments(eq(TARGET_NAME), eq(TARGET_ID)).thenThrow(new CreationException());
whenNew(Target.class).withArguments(eq("Unknown"), eq(-1)).thenReturn(expectedTarget);
Target actualTarget = new ExpectNewDemo().createTarget(new ITarget() {
@Override
public int getId() {
return TARGET_ID;
}
@Override
public String getName() {
return TARGET_NAME;
}
});
assertThat(actualTarget).isEqualToComparingFieldByField(expectedTarget);
}
Aggregations