Search in sources :

Example 66 with MyClass

use of samples.newmocking.MyClass 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 67 with MyClass

use of samples.newmocking.MyClass in project powermock by powermock.

the class ReplayAllForExpectNewTest method testGetMessage.

@Test
public void testGetMessage() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock = createMock(MyClass.class);
    expectNew(MyClass.class).andReturn(myClassMock);
    String expected = "Hello altered World";
    expect(myClassMock.getMessage()).andReturn("Hello altered World");
    replayAll();
    String actual = tested.getMessage();
    verifyAll();
    assertEquals("Expected and actual did not match", expected, actual);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 68 with MyClass

use of samples.newmocking.MyClass in project powermock by powermock.

the class ResetMockTest method assertManualResetWorksWhenMixingInstanceAndClassMocks.

@Test
public void assertManualResetWorksWhenMixingInstanceAndClassMocks() 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, 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 69 with MyClass

use of samples.newmocking.MyClass in project powermock by powermock.

the class ResetMockTest method assertResetAllWorks.

@Test
public void assertResetAllWorks() 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);
    resetAll();
    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 70 with MyClass

use of samples.newmocking.MyClass in project powermock by powermock.

the class ExpectNewDemoUsingThePrepareEverythingAnnotationTest method testSimpleSingleNew_withOnce.

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

Aggregations

MyClass (samples.newmocking.MyClass)93 Test (org.junit.Test)91 ExpectNewDemo (samples.expectnew.ExpectNewDemo)75 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)49 PrepareEverythingForTest (org.powermock.core.classloader.annotations.PrepareEverythingForTest)21 StupidNew (samples.newmocking.StupidNew)3 MockitoAssertionError (org.mockito.exceptions.base.MockitoAssertionError)2