use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class WhenNewCases method whenNewSupportsVarArgsAsSecondParameter.
@Test
public void whenNewSupportsVarArgsAsSecondParameter() throws Exception {
final int one = 1;
final int two = 2;
final float myFloat = 3.0f;
ExpectNewDemo tested = new ExpectNewDemo();
VarArgsConstructorDemo varArgsConstructorDemoMock = mock(VarArgsConstructorDemo.class);
whenNew(VarArgsConstructorDemo.class).withArguments(myFloat, one, two).thenReturn(varArgsConstructorDemoMock);
when(varArgsConstructorDemoMock.getInts()).thenReturn(new int[] { one, two });
int[] varArgs = tested.newVarArgs(myFloat, one, two);
assertEquals(2, varArgs.length);
assertEquals(one, varArgs[0]);
assertEquals(two, varArgs[1]);
verifyNew(VarArgsConstructorDemo.class).withArguments(myFloat, one, two);
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class WhenNewCases method testSimpleMultipleNewPrivate_tooManyTimesExpected.
@Test
public void testSimpleMultipleNewPrivate_tooManyTimesExpected() throws Exception {
ExpectNewDemo tested = new ExpectNewDemo();
MyClass myClassMock1 = mock(MyClass.class);
whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock1);
Whitebox.invokeMethod(tested, "simpleMultipleNewPrivate");
try {
verifyNew(MyClass.class, times(4)).withNoArguments();
fail("Should throw an exception!.");
} catch (AssertionError e) {
assertEquals("samples.newmocking.MyClass();\nWanted 4 times but was 3 times.", e.getMessage());
}
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class WhenNewCases method testNewWithParameterTypesAndArguments.
@Test
public void testNewWithParameterTypesAndArguments() 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).withParameterTypes(Service.class, int.class).withArguments(serviceMock, numberOfTimes).thenReturn(expectNewServiceImplMock);
when(expectNewServiceImplMock.useService()).thenReturn(expected);
assertEquals(expected, tested.newWithArguments(serviceMock, numberOfTimes));
verifyNew(ExpectNewServiceUser.class).withArguments(serviceMock, numberOfTimes);
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class GetClassTest method getClassReturnsTheCorrectClassForMocksPrepareForTest.
@Test
public void getClassReturnsTheCorrectClassForMocksPrepareForTest() throws Exception {
ExpectNewDemo instance = mock(ExpectNewDemo.class);
assertNotNull(instance.getClass());
}
use of samples.expectnew.ExpectNewDemo in project powermock by powermock.
the class GetClassTest method getClassReturnsNullForMocksPreparedForTestWhenMockingOfGetClassAllowed.
@Test
public void getClassReturnsNullForMocksPreparedForTestWhenMockingOfGetClassAllowed() throws Exception {
MockGateway.MOCK_GET_CLASS_METHOD = true;
ExpectNewDemo instance = mock(ExpectNewDemo.class);
try {
assertNull(instance.getClass());
} finally {
// Make sure we reset to the default for subsequent tests.
MockGateway.MOCK_GET_CLASS_METHOD = false;
}
}
Aggregations