Search in sources :

Example 16 with Service

use of samples.Service in project powermock by powermock.

the class ReplayAllForExpectNewTest method testReplayAllWithAdditionalMocks.

@Test
public void testReplayAllWithAdditionalMocks() throws Exception {
    final int numberOfTimes = 2;
    final String expected = "used";
    ExpectNewDemo tested = new ExpectNewDemo();
    ExpectNewServiceUser expectNewServiceImplMock = EasyMock.createMock(ExpectNewServiceUser.class);
    Service serviceMock = createMock(Service.class);
    expectNew(ExpectNewServiceUser.class, serviceMock, numberOfTimes).andReturn(expectNewServiceImplMock);
    expect(expectNewServiceImplMock.useService()).andReturn(expected);
    replayAll(expectNewServiceImplMock);
    assertEquals(expected, tested.newWithArguments(serviceMock, numberOfTimes));
    verifyAll();
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) ExpectNewServiceUser(samples.expectnew.ExpectNewServiceUser) Service(samples.Service) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 17 with Service

use of samples.Service in project powermock by powermock.

the class ExpectNewDemoUsingThePrepareEverythingAnnotationTest method testNewWithVarArgsConstructorWhenOneArgumentIsOfASubType.

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

        @Override
        public String getServiceMessage() {
            return "message";
        }
    };
    expectNew(VarArgsConstructorDemo.class, serviceSubTypeInstance, serviceMock).andReturn(varArgsConstructorDemoMock);
    expect(varArgsConstructorDemoMock.getAllServices()).andReturn(new Service[] { serviceMock });
    replayAll();
    Service[] varArgs = tested.newVarArgs(serviceSubTypeInstance, serviceMock);
    assertEquals(1, varArgs.length);
    assertSame(serviceMock, varArgs[0]);
    verifyAll();
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) VarArgsConstructorDemo(samples.expectnew.VarArgsConstructorDemo) Service(samples.Service) Test(org.junit.Test) PrepareEverythingForTest(org.powermock.core.classloader.annotations.PrepareEverythingForTest)

Example 18 with Service

use of samples.Service in project powermock by powermock.

the class ExpectNewDemoUsingThePrepareEverythingAnnotationTest method testNewWithWrongArgument.

@Test
public void testNewWithWrongArgument() throws Exception {
    final int numberOfTimes = 2;
    final String expected = "used";
    ExpectNewDemo tested = new ExpectNewDemo();
    ExpectNewServiceUser expectNewServiceImplMock = createMock(ExpectNewServiceUser.class);
    Service serviceMock = createMock(Service.class);
    expectNew(ExpectNewServiceUser.class, serviceMock, numberOfTimes).andReturn(expectNewServiceImplMock);
    expect(expectNewServiceImplMock.useService()).andReturn(expected);
    replayAll();
    try {
        assertEquals(expected, tested.newWithWrongArguments(serviceMock, numberOfTimes));
        verifyAll();
        fail("Should throw AssertionError!");
    } catch (AssertionError e) {
        assertEquals("\n  Unexpected constructor call samples.expectnew.ExpectNewServiceUser(EasyMock for interface samples.Service, 4):" + "\n    samples.expectnew.ExpectNewServiceUser(EasyMock for interface samples.Service, 2): expected: 1, actual: 0", e.getMessage());
    }
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) ExpectNewServiceUser(samples.expectnew.ExpectNewServiceUser) Service(samples.Service) Test(org.junit.Test) PrepareEverythingForTest(org.powermock.core.classloader.annotations.PrepareEverythingForTest)

Example 19 with Service

use of samples.Service in project powermock by powermock.

the class MockSelfPrivateFieldServiceClassTest method testGetCompositeMessage.

@Test
public void testGetCompositeMessage() throws Exception {
    MockSelfPrivateFieldServiceClass tested = createPartialMock(MockSelfPrivateFieldServiceClass.class, "getOwnMessage");
    Service serviceMock = createMock(Service.class);
    setInternalState(tested, "service", serviceMock, MockSelfPrivateFieldServiceClass.class);
    final String expected = "Hello world";
    expectPrivate(tested, "getOwnMessage").andReturn("Hello");
    expect(serviceMock.getServiceMessage()).andReturn(" world");
    replay(serviceMock);
    replay(tested);
    final String actual = tested.getCompositeMessage();
    verify(serviceMock);
    verify(tested);
    assertEquals(expected, actual);
}
Also used : MockSelfPrivateFieldServiceClass(samples.privatefield.MockSelfPrivateFieldServiceClass) Service(samples.Service) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 20 with Service

use of samples.Service in project powermock by powermock.

the class SimplePrivateFieldServiceClassTest method testSimplePrivateFieldServiceClass.

@Test
public void testSimplePrivateFieldServiceClass() throws Exception {
    SimplePrivateFieldServiceClass tested = new SimplePrivateFieldServiceClass();
    Service serviceMock = createMock(Service.class);
    setInternalState(tested, "service", serviceMock, SimplePrivateFieldServiceClass.class);
    final String expected = "Hello world!";
    expect(serviceMock.getServiceMessage()).andReturn(expected);
    replay(serviceMock);
    final String actual = tested.useService();
    verify(serviceMock);
    assertEquals(expected, actual);
}
Also used : Service(samples.Service) SimplePrivateFieldServiceClass(samples.privatefield.SimplePrivateFieldServiceClass) 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