Search in sources :

Example 86 with ExpectNewDemo

use of samples.expectnew.ExpectNewDemo in project powermock by powermock.

the class WhenNewCases method testSimpleSingleNew_withOnce.

@Test
public void testSimpleSingleNew_withOnce() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock1 = mock(MyClass.class);
    whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock1);
    tested.simpleSingleNew();
    verifyNew(MyClass.class).withNoArguments();
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 87 with ExpectNewDemo

use of samples.expectnew.ExpectNewDemo 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 88 with ExpectNewDemo

use of samples.expectnew.ExpectNewDemo in project powermock by powermock.

the class WhenNewCases method testSimpleMultipleNew.

@Test
public void testSimpleMultipleNew() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock = mock(MyClass.class);
    whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock);
    tested.simpleMultipleNew();
    verifyNew(MyClass.class, times(3)).withNoArguments();
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 89 with ExpectNewDemo

use of samples.expectnew.ExpectNewDemo in project powermock by powermock.

the class WhenNewCases method testSimpleMultipleNew_withAtLeastOnce.

@Test
public void testSimpleMultipleNew_withAtLeastOnce() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock1 = mock(MyClass.class);
    whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock1);
    tested.simpleMultipleNew();
    verifyNew(MyClass.class, atLeastOnce()).withNoArguments();
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 90 with ExpectNewDemo

use of samples.expectnew.ExpectNewDemo in project powermock by powermock.

the class WhenNewCases method canDefineSeveralMockResultForNew.

@Test
public void canDefineSeveralMockResultForNew() throws Exception {
    final Target expectedTarget = new Target(UNKNOWN_TARGET_NAME, UNKNOWN_TARGET_ID);
    whenNew(Target.class).withArguments(eq(TARGET_NAME), eq(TARGET_ID)).thenThrow(new CreationException());
    whenNew(Target.class).withArguments(eq("Unknown"), eq(-1)).thenReturn(expectedTarget);
    Target actualTarget = new ExpectNewDemo().createTarget(new ITarget() {

        @Override
        public int getId() {
            return TARGET_ID;
        }

        @Override
        public String getName() {
            return TARGET_NAME;
        }
    });
    assertThat(actualTarget).isEqualToComparingFieldByField(expectedTarget);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) ITarget(samples.expectnew.ITarget) Target(samples.expectnew.Target) ITarget(samples.expectnew.ITarget) CreationException(samples.expectnew.CreationException) 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