Search in sources :

Example 96 with ExpectNewDemo

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

the class WhenNewCases method testNewWithArrayVarArgsWhenAllArgumentsAreNull.

@Test
public void testNewWithArrayVarArgsWhenAllArgumentsAreNull() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    SimpleVarArgsConstructorDemo varArgsConstructorDemoMock = mock(SimpleVarArgsConstructorDemo.class);
    final byte[] byteArrayOne = null;
    final byte[] byteArrayTwo = null;
    whenNew(SimpleVarArgsConstructorDemo.class).withArguments(byteArrayOne, byteArrayTwo).thenReturn(varArgsConstructorDemoMock);
    when(varArgsConstructorDemoMock.getByteArrays()).thenReturn(new byte[][] { byteArrayTwo });
    byte[][] varArgs = tested.newSimpleVarArgs(byteArrayOne, byteArrayTwo);
    assertEquals(1, varArgs.length);
    assertSame(byteArrayTwo, varArgs[0]);
    verifyNew(SimpleVarArgsConstructorDemo.class).withArguments(byteArrayOne, byteArrayTwo);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) SimpleVarArgsConstructorDemo(samples.expectnew.SimpleVarArgsConstructorDemo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 97 with ExpectNewDemo

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

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

the class WhenNewCases method testSimpleMultipleNew_tooFewTimesExpected.

@Test
public void testSimpleMultipleNew_tooFewTimesExpected() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock1 = mock(MyClass.class);
    whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock1);
    tested.simpleMultipleNew();
    try {
        verifyNew(MyClass.class, times(1)).withNoArguments();
        fail("Should throw AssertionError.");
    } catch (AssertionError e) {
        assertEquals("samples.newmocking.MyClass();\nWanted 1 time but was 3 times.", e.getMessage());
    }
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 99 with ExpectNewDemo

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

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

the class ReplayAllForExpectNewTest 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));
    replayAll();
    try {
        tested.throwExceptionAndWrapInRunTimeWhenInvoction();
        fail("Should throw a checked Exception!");
    } catch (RuntimeException e) {
        assertTrue(e.getCause() instanceof IOException);
        assertEquals(expectedFailMessage, e.getMessage());
    }
    verifyAll();
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) IOException(java.io.IOException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

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