Search in sources :

Example 51 with ExpectNewDemo

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);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) VarArgsConstructorDemo(samples.expectnew.VarArgsConstructorDemo) SimpleVarArgsConstructorDemo(samples.expectnew.SimpleVarArgsConstructorDemo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 52 with ExpectNewDemo

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());
    }
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 53 with ExpectNewDemo

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);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) ExpectNewServiceUser(samples.expectnew.ExpectNewServiceUser) Service(samples.Service) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 54 with ExpectNewDemo

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());
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 55 with ExpectNewDemo

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;
    }
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)143 ExpectNewDemo (samples.expectnew.ExpectNewDemo)143 MyClass (samples.newmocking.MyClass)86 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)82 VarArgsConstructorDemo (samples.expectnew.VarArgsConstructorDemo)33 PrepareEverythingForTest (org.powermock.core.classloader.annotations.PrepareEverythingForTest)30 Service (samples.Service)17 ExpectNewServiceUser (samples.expectnew.ExpectNewServiceUser)13 SimpleVarArgsConstructorDemo (samples.expectnew.SimpleVarArgsConstructorDemo)10 IOException (java.io.IOException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 DataInputStream (java.io.DataInputStream)4 InputStream (java.io.InputStream)4 CreationException (samples.expectnew.CreationException)2 ITarget (samples.expectnew.ITarget)2 Target (samples.expectnew.Target)2 Date (java.util.Date)1 MockitoAssertionError (org.mockito.exceptions.base.MockitoAssertionError)1