Search in sources :

Example 31 with ExpectNewDemo

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

the class ExpectNewDemoUsingThePrepareEverythingAnnotationTest method testNewWithArrayVarArgsWhenFirstArgumentIsNotNullButSubseqentArgumentsAreNull.

@Test
public void testNewWithArrayVarArgsWhenFirstArgumentIsNotNullButSubseqentArgumentsAreNull() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    VarArgsConstructorDemo varArgsConstructorDemoMock = createMock(VarArgsConstructorDemo.class);
    final byte[] byteArrayOne = new byte[] { 42 };
    final byte[] byteArrayTwo = null;
    expectNew(VarArgsConstructorDemo.class, byteArrayOne, byteArrayTwo).andReturn(varArgsConstructorDemoMock);
    expect(varArgsConstructorDemoMock.getByteArrays()).andReturn(new byte[][] { byteArrayOne });
    replayAll();
    byte[][] varArgs = tested.newVarArgs(byteArrayOne, byteArrayTwo);
    assertEquals(1, varArgs.length);
    assertSame(byteArrayOne, varArgs[0]);
    verifyAll();
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) VarArgsConstructorDemo(samples.expectnew.VarArgsConstructorDemo) Test(org.junit.Test) PrepareEverythingForTest(org.powermock.core.classloader.annotations.PrepareEverythingForTest)

Example 32 with ExpectNewDemo

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

the class ExpectNewDemoUsingThePrepareEverythingAnnotationTest method testSimpleMultipleNew_tooManyTimesExpected.

@Test
public void testSimpleMultipleNew_tooManyTimesExpected() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock1 = createMock(MyClass.class);
    expectNew(MyClass.class).andReturn(myClassMock1).times(4);
    replayAll();
    tested.simpleMultipleNew();
    try {
        verifyAll();
        fail("Should throw AssertionError.");
    } catch (AssertionError e) {
        assertEquals("\n  Expectation failure on verify:" + "\n    samples.newmocking.MyClass(): expected: 4, actual: 3", e.getMessage());
    }
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) Test(org.junit.Test) PrepareEverythingForTest(org.powermock.core.classloader.annotations.PrepareEverythingForTest)

Example 33 with ExpectNewDemo

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

the class ResetMockTest method assertManualResetWorks.

@Test
public void assertManualResetWorks() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock = createMock(MyClass.class);
    expectNew(MyClass.class).andReturn(myClassMock);
    expect(myClassMock.getMessage()).andReturn("message");
    replayAll();
    String message = tested.getMessage();
    verifyAll();
    assertEquals("message", message);
    reset(myClassMock);
    reset(MyClass.class);
    expectNew(MyClass.class).andReturn(myClassMock);
    expect(myClassMock.getMessage()).andReturn("message");
    replayAll();
    message = tested.getMessage();
    verifyAll();
    assertEquals("message", message);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 34 with ExpectNewDemo

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

the class ExpectNewDemoUsingThePrepareEverythingAnnotationTest 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);
    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) PrepareEverythingForTest(org.powermock.core.classloader.annotations.PrepareEverythingForTest)

Example 35 with ExpectNewDemo

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

the class ExpectNewDemoUsingThePrepareEverythingAnnotationTest method testSimpleMultipleNew.

@Test
public void testSimpleMultipleNew() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock1 = createMock(MyClass.class);
    expectNew(MyClass.class).andReturn(myClassMock1).times(3);
    replayAll();
    tested.simpleMultipleNew();
    verifyAll();
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) Test(org.junit.Test) PrepareEverythingForTest(org.powermock.core.classloader.annotations.PrepareEverythingForTest)

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