Search in sources :

Example 6 with Service

use of samples.Service in project powermock by powermock.

the class WhenNewCases method whenNewAnyArgumentsWorksInClassesWithMultipleCtors.

@Test
public void whenNewAnyArgumentsWorksInClassesWithMultipleCtors() throws Exception {
    ExpectNewWithMultipleCtorDemo expectNewWithMultipleCtorDemoMock = mock(ExpectNewWithMultipleCtorDemo.class);
    Service serviceMock = mock(Service.class);
    whenNew(ExpectNewWithMultipleCtorDemo.class).withAnyArguments().thenReturn(expectNewWithMultipleCtorDemoMock);
    when(expectNewWithMultipleCtorDemoMock.useService()).thenReturn("message");
    // When
    final ExpectNewWithMultipleCtorDemo expectNewWithMultipleCtorDemo = new ExpectNewWithMultipleCtorDemo(serviceMock);
    final String message1 = expectNewWithMultipleCtorDemo.useService();
    final ExpectNewWithMultipleCtorDemo expectNewWithMultipleCtorDemo1 = new ExpectNewWithMultipleCtorDemo(serviceMock, 5);
    final String message2 = expectNewWithMultipleCtorDemo1.useService();
    assertEquals(message1, "message");
    assertEquals(message2, "message");
}
Also used : ExpectNewWithMultipleCtorDemo(samples.expectnew.ExpectNewWithMultipleCtorDemo) Service(samples.Service) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with Service

use of samples.Service in project powermock by powermock.

the class WhenNewCases method testNewWithArguments.

@Test
public void testNewWithArguments() 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.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 8 with Service

use of samples.Service in project powermock by powermock.

the class WhenNewCases method testNewUsingConstructorWithArguments.

@Test
public void testNewUsingConstructorWithArguments() 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(constructor(ExpectNewServiceUser.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 9 with Service

use of samples.Service 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 10 with Service

use of samples.Service in project powermock by powermock.

the class WhenNewCases method testNewWithVarArgsConstructorWhenOneArgumentIsOfASubType.

@Test
public void testNewWithVarArgsConstructorWhenOneArgumentIsOfASubType() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    Service serviceMock = mock(Service.class);
    VarArgsConstructorDemo varArgsConstructorDemoMock = mock(VarArgsConstructorDemo.class);
    final Service serviceSubTypeInstance = new Service() {

        public String getServiceMessage() {
            return "message";
        }
    };
    whenNew(VarArgsConstructorDemo.class).withArguments(serviceSubTypeInstance, serviceMock).thenReturn(varArgsConstructorDemoMock);
    when(varArgsConstructorDemoMock.getAllServices()).thenReturn(new Service[] { serviceMock });
    Service[] varArgs = tested.newVarArgs(serviceSubTypeInstance, serviceMock);
    assertEquals(1, varArgs.length);
    assertSame(serviceMock, varArgs[0]);
    verifyNew(VarArgsConstructorDemo.class).withArguments(serviceSubTypeInstance, serviceMock);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) VarArgsConstructorDemo(samples.expectnew.VarArgsConstructorDemo) SimpleVarArgsConstructorDemo(samples.expectnew.SimpleVarArgsConstructorDemo) Service(samples.Service) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)22 Service (samples.Service)22 ExpectNewDemo (samples.expectnew.ExpectNewDemo)17 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)13 ExpectNewServiceUser (samples.expectnew.ExpectNewServiceUser)13 VarArgsConstructorDemo (samples.expectnew.VarArgsConstructorDemo)4 PrepareEverythingForTest (org.powermock.core.classloader.annotations.PrepareEverythingForTest)3 SimplePrivateFieldServiceClass (samples.privatefield.SimplePrivateFieldServiceClass)3 ExpectNewWithMultipleCtorDemo (samples.expectnew.ExpectNewWithMultipleCtorDemo)1 SimpleVarArgsConstructorDemo (samples.expectnew.SimpleVarArgsConstructorDemo)1 MockSelfPrivateFieldServiceClass (samples.privatefield.MockSelfPrivateFieldServiceClass)1