Search in sources :

Example 11 with Service

use of samples.Service in project powermock by powermock.

the class ExpectNewDemoTest 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);
    replay(expectNewServiceImplMock, serviceMock, ExpectNewServiceUser.class);
    try {
        assertEquals(expected, tested.newWithWrongArguments(serviceMock, numberOfTimes));
        verify(expectNewServiceImplMock, serviceMock, ExpectNewServiceUser.class);
        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) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with Service

use of samples.Service 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!");
}
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 13 with Service

use of samples.Service in project powermock by powermock.

the class WhenNewCases method testNewWithConstructorUsingParameterTypesAndArguments.

@Test
public void testNewWithConstructorUsingParameterTypesAndArguments() 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, 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 14 with Service

use of samples.Service in project powermock by powermock.

the class ExpectNewCases method testNewWithArguments.

@Test
public void testNewWithArguments() 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);
    replay(expectNewServiceImplMock, serviceMock, ExpectNewServiceUser.class);
    assertEquals(expected, tested.newWithArguments(serviceMock, numberOfTimes));
    verify(expectNewServiceImplMock, serviceMock, ExpectNewServiceUser.class);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) ExpectNewServiceUser(samples.expectnew.ExpectNewServiceUser) Service(samples.Service) Test(org.junit.Test)

Example 15 with Service

use of samples.Service in project powermock by powermock.

the class ReplayAllForExpectNewTest method testReplayAllWithExpectNewWhenTheClassBeingConstructedIsNotPreparedForTest.

@Test
public void testReplayAllWithExpectNewWhenTheClassBeingConstructedIsNotPreparedForTest() 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();
    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)

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