Search in sources :

Example 6 with ExpectNewDemo

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

the class ExpectNewDemoTest 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 });
    replay(serviceMock, VarArgsConstructorDemo.class, varArgsConstructorDemoMock);
    Service[] varArgs = tested.newVarArgs(serviceSubTypeInstance, serviceMock);
    assertEquals(1, varArgs.length);
    assertSame(serviceMock, varArgs[0]);
    verify(serviceMock, VarArgsConstructorDemo.class, varArgsConstructorDemoMock);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) VarArgsConstructorDemo(samples.expectnew.VarArgsConstructorDemo) Service(samples.Service) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with ExpectNewDemo

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

the class ExpectNewDemoTest method testInvokeVoidMethod.

@Test
public void testInvokeVoidMethod() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock = createMock(MyClass.class);
    expectNew(MyClass.class).andReturn(myClassMock);
    myClassMock.voidMethod();
    expectLastCall().times(1);
    replay(myClassMock, MyClass.class);
    tested.invokeVoidMethod();
    verify(myClassMock, MyClass.class);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with ExpectNewDemo

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

the class ExpectNewDemoTest method testNewWithCheckedException.

@Test
public void testNewWithCheckedException() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    final String expectedFailMessage = "testing checked exception";
    expectNew(MyClass.class).andThrow(new IOException(expectedFailMessage));
    replay(MyClass.class);
    try {
        tested.throwExceptionAndWrapInRunTimeWhenInvoction();
        fail("Should throw a checked Exception!");
    } catch (RuntimeException e) {
        assertTrue(e.getCause() instanceof IOException);
        assertEquals(expectedFailMessage, e.getMessage());
    }
    verify(MyClass.class);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with ExpectNewDemo

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

the class ExpectNewDemoTest method testSimpleMultipleNew_withRange_OK.

@Test
public void testSimpleMultipleNew_withRange_OK() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock1 = createMock(MyClass.class);
    expectNew(MyClass.class).andReturn(myClassMock1).times(1, 5);
    replay(myClassMock1, MyClass.class);
    tested.simpleMultipleNew();
    verify(myClassMock1, MyClass.class);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 10 with ExpectNewDemo

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

the class ExpectNewCases method testExpectNewButNoNewCallWasMade.

@Test
public void testExpectNewButNoNewCallWasMade() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock1 = createMock(MyClass.class);
    expectNew(MyClass.class).andReturn(myClassMock1).once();
    replay(myClassMock1, MyClass.class);
    try {
        tested.makeDate();
        verify(myClassMock1, MyClass.class);
        fail("Should throw AssertionError!");
    } catch (AssertionError e) {
        assertTrue(e.getMessage().contains(MyClass.class.getName() + "(): expected: 1, actual: 0"));
    }
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) 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