Package samples.expectnew

Examples of samples.expectnew.ExpectNewDemo


        verifyNew(MyClass.class).withNoArguments();
    }

    @Test
    public void testNewWithRuntimeException() throws Exception {
        ExpectNewDemo tested = new ExpectNewDemo();

        final String expectedFailMessage = "testing";
        whenNew(MyClass.class).withNoArguments().thenThrow(new RuntimeException(expectedFailMessage));

        try {
            tested.throwExceptionWhenInvoction();
            fail("Should throw RuntimeException!");
        } catch (RuntimeException e) {
            assertEquals(expectedFailMessage, e.getMessage());
        }
View Full Code Here


        verifyNew(MyClass.class).withNoArguments();
    }

    @Test
    public void testMultipleNew() throws Exception {
        ExpectNewDemo tested = new ExpectNewDemo();

        MyClass myClassMock = mock(MyClass.class);

        whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock);

        when(myClassMock.getMessage()).thenReturn("Hello");

        final String actual = tested.multipleNew();

        verify(myClassMock, times(2)).getMessage();
        verifyNew(MyClass.class, times(2)).withNoArguments();

        assertEquals("HelloHello", actual);
View Full Code Here

        assertEquals("HelloHello", actual);
    }

    @Test
    public void testSimpleMultipleNew() throws Exception {
        ExpectNewDemo tested = new ExpectNewDemo();

        MyClass myClassMock = mock(MyClass.class);

        whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock);

        tested.simpleMultipleNew();

        verifyNew(MyClass.class, times(3)).withNoArguments();
    }
View Full Code Here

        assertEquals("Expected and actual did not match", expected, actual);
    }

    @Test
    public void testGetMessageWithArgument() throws Exception {
        ExpectNewDemo tested = new ExpectNewDemo();

        MyClass myClassMock = createMock(MyClass.class);
        expectNew(MyClass.class).andReturn(myClassMock);

        String expected = "Hello altered World";
        expect(myClassMock.getMessage("test")).andReturn("Hello altered World");
        replayAll();

        String actual = tested.getMessageWithArgument();

        verifyAll();
        assertEquals("Expected and actual did not match", expected, actual);
    }
View Full Code Here

        assertEquals("Expected and actual did not match", expected, actual);
    }

    @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);

        replayAll();

        tested.invokeVoidMethod();

        verifyAll();
    }
View Full Code Here

        verifyAll();
    }

    @Test
    public void testNewWithRuntimeException() throws Exception {
        ExpectNewDemo tested = new ExpectNewDemo();

        final String expectedFailMessage = "testing";
        expectNew(MyClass.class).andThrow(new RuntimeException(expectedFailMessage));

        replayAll();

        try {
            tested.throwExceptionWhenInvoction();
            fail("Should throw RuntimeException!");
        } catch (RuntimeException e) {
            assertEquals(expectedFailMessage, e.getMessage());
        }
View Full Code Here

        verifyAll();
    }

    @Test
    public void testMultipleNew() throws Exception {
        ExpectNewDemo tested = new ExpectNewDemo();

        MyClass myClassMock1 = createMock(MyClass.class);
        MyClass myClassMock2 = createMock(MyClass.class);

        expectNew(MyClass.class).andReturn(myClassMock1);
        expectNew(MyClass.class).andReturn(myClassMock2);

        expect(myClassMock1.getMessage()).andReturn("Hello ");
        expect(myClassMock2.getMessage()).andReturn("World");

        replayAll();

        final String actual = tested.multipleNew();

        verifyAll();

        assertEquals("Hello World", actual);
    }
View Full Code Here

        assertEquals("Hello World", actual);
    }

    @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();
    }
View Full Code Here

        verifyAll();
    }

    @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) {
View Full Code Here

        }
    }

    @Test
    public void testSimpleMultipleNew_tooFewTimesExpected() throws Exception {
        ExpectNewDemo tested = new ExpectNewDemo();

        MyClass myClassMock1 = createMock(MyClass.class);

        expectNew(MyClass.class).andReturn(myClassMock1).times(2);

        replayAll();
        try {
            tested.simpleMultipleNew();
            fail("Should throw AssertionError.");
        } catch (AssertionError e) {
            assertEquals("\n  Unexpected constructor call samples.newmocking.MyClass():"
                    + "\n    samples.newmocking.MyClass(): expected: 2, actual: 3", e.getMessage());
        }
View Full Code Here

TOP

Related Classes of samples.expectnew.ExpectNewDemo

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.