Search in sources :

Example 41 with ExpectNewDemo

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

the class VerifyNoMoreInteractionsTest method verifyNoMoreInteractionsOnNewInstancesThrowsAssertionErrorWhenMoreInteractionsTookPlace.

@Test
public void verifyNoMoreInteractionsOnNewInstancesThrowsAssertionErrorWhenMoreInteractionsTookPlace() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock = mock(MyClass.class);
    whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock);
    tested.simpleMultipleNew();
    try {
        verifyNoMoreInteractions(MyClass.class);
        fail("Should throw exception!");
    } catch (MockitoAssertionError e) {
        assertTrue(e.getMessage().startsWith("\nNo interactions wanted here:\n-> at samples.powermockito.junit4.verifynomoreinteractions.VerifyNoMoreInteractionsTest.verifyNoMoreInteractionsOnNewInstancesThrowsAssertionErrorWhenMoreInteractionsTookPlace(VerifyNoMoreInteractionsTest.java:"));
    }
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) MockitoAssertionError(org.mockito.exceptions.base.MockitoAssertionError) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 42 with ExpectNewDemo

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

the class VerifyNewWithoutWhenNewTest method verifyingNewWithoutExpectationWhenNoArgumentsThrowsISE.

@Test
public // TODO This should actually work in the future when issue 148 is resolved.
void verifyingNewWithoutExpectationWhenNoArgumentsThrowsISE() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    assertEquals("Hello world", tested.getMessage());
    try {
        verifyNew(MyClass.class).withNoArguments();
        fail("IllegalStateException expected");
    } catch (IllegalStateException e) {
        assertEquals("No instantiation of class samples.newmocking.MyClass was recorded " + "during the test. Note that only expected object creations " + "(e.g. those using whenNew(..)) can be verified.", e.getMessage());
    }
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 43 with ExpectNewDemo

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

the class VerifyNewWithoutWhenNewTest method verifyingNewWithoutExpectationButWithArgumentsThrowsISE.

@Test
public // TODO This should actually work in the future when issue 148 is resolved.
void verifyingNewWithoutExpectationButWithArgumentsThrowsISE() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    assertEquals("Hello world", tested.getMessage());
    try {
        verifyNew(MyClass.class, Mockito.atLeastOnce()).withNoArguments();
        fail("IllegalStateException expected");
    } catch (IllegalStateException e) {
        assertEquals("No instantiation of class samples.newmocking.MyClass was recorded " + "during the test. Note that only expected object creations " + "(e.g. those using whenNew(..)) can be verified.", e.getMessage());
    }
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 44 with ExpectNewDemo

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

the class WhenNewCases method testInvokeVoidMethod.

@Test
public void testInvokeVoidMethod() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock = mock(MyClass.class);
    whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock);
    doNothing().when(myClassMock).voidMethod();
    tested.invokeVoidMethod();
    verify(myClassMock).voidMethod();
    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 45 with ExpectNewDemo

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

the class WhenNewCases method testNewWithArrayVarArgsAndMatchers.

@Test
public void testNewWithArrayVarArgsAndMatchers() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    VarArgsConstructorDemo varArgsConstructorDemoMock = mock(VarArgsConstructorDemo.class);
    final byte[] byteArrayOne = new byte[] { 42 };
    final byte[] byteArrayTwo = new byte[] { 17 };
    whenNew(VarArgsConstructorDemo.class).withArguments(byteArrayOne, byteArrayTwo).thenReturn(varArgsConstructorDemoMock);
    when(varArgsConstructorDemoMock.getByteArrays()).thenReturn(new byte[][] { byteArrayOne });
    byte[][] varArgs = tested.newVarArgsWithMatchers();
    assertEquals(1, varArgs.length);
    assertSame(byteArrayOne, varArgs[0]);
    verifyNew(VarArgsConstructorDemo.class).withArguments(byteArrayOne, byteArrayTwo);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) VarArgsConstructorDemo(samples.expectnew.VarArgsConstructorDemo) SimpleVarArgsConstructorDemo(samples.expectnew.SimpleVarArgsConstructorDemo) 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