Package samples.expectnew

Examples of samples.expectnew.ExpectNewDemo


        }
    }

    @Test
    public void testAlternativeFlow() throws Exception {
        ExpectNewDemo tested = new ExpectNewDemo();
        expectNew(DataInputStream.class, new Object[] { null }).andThrow(new RuntimeException("error"));

        replayAll();

        InputStream stream = tested.alternativePath();

        verifyAll();

        assertNotNull("The returned inputstream should not be null.", stream);
        assertTrue("The returned inputstream should be an instance of ByteArrayInputStream.", stream instanceof ByteArrayInputStream);
View Full Code Here


@PrepareForTest( { MyClass.class, ExpectNewDemo.class, DataInputStream.class })
public class ExpectNewDemoTest {

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

        final String expectedFailMessage = "testing checked exception";
        expectNew(MyClass.class).andThrow(new IOException(expectedFailMessage));

        replay(MyClass.class);

        try {
            tested.throwExceptionAndWrapInRunTimeWhenInvoction();
            fail("Should throw a checked Exception!");
        } catch (RuntimeException e) {
            assertTrue(e.getCause() instanceof IOException);
            assertEquals(expectedFailMessage, e.getMessage());
        }
View Full Code Here

        assertTrue("The returned inputstream should be an instance of ByteArrayInputStream.", stream instanceof ByteArrayInputStream);
    }

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

        MyClass myClassMock1 = createMock(MyClass.class);

        expectNew(MyClass.class).andReturn(myClassMock1).times(4);
View Full Code Here

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

        verify(MyClass.class);
    }

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

        replay(myClassMock, MyClass.class);

        String actual = tested.getMessage();

        verify(myClassMock, MyClass.class);
        assertEquals("Expected and actual did not match", expected, actual);
    }
View Full Code Here

    @Test
    public void testNewWithVarArgs() throws Exception {
        final String firstString = "hello";
        final String secondString = "world";

        ExpectNewDemo tested = new ExpectNewDemo();
        VarArgsConstructorDemo varArgsConstructorDemoMock = createMock(VarArgsConstructorDemo.class);

        expectNew(VarArgsConstructorDemo.class, firstString, secondString).andReturn(varArgsConstructorDemoMock);
        expect(varArgsConstructorDemoMock.getAllMessages()).andReturn(new String[] { firstString, secondString });

        replayAll();

        String[] varArgs = tested.newVarArgs(firstString, secondString);
        assertEquals(2, varArgs.length);
        assertEquals(firstString, varArgs[0]);
        assertEquals(secondString, varArgs[1]);

        verifyAll();
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");
        replay(myClassMock, MyClass.class);

        String actual = tested.getMessageWithArgument();

        verify(myClassMock, MyClass.class);
        assertEquals("Expected and actual did not match", expected, actual);
    }
View Full Code Here

        }
    }

    @Test
    public void testNewWithVarArgsConstructorWhenOneArgumentIsOfASubType() throws Exception {
        ExpectNewDemo tested = new ExpectNewDemo();
        Service serviceMock = createMock(Service.class);
        VarArgsConstructorDemo varArgsConstructorDemoMock = createMock(VarArgsConstructorDemo.class);

        final Service serviceSubTypeInstance = new Service() {

            public String getServiceMessage() {
                return "message";
            }
        };

        expectNew(VarArgsConstructorDemo.class, serviceSubTypeInstance, serviceMock).andReturn(varArgsConstructorDemoMock);
        expect(varArgsConstructorDemoMock.getAllServices()).andReturn(new Service[] { serviceMock });

        replayAll();

        Service[] varArgs = tested.newVarArgs(serviceSubTypeInstance, serviceMock);
        assertEquals(1, varArgs.length);
        assertSame(serviceMock, varArgs[0]);

        verifyAll();
    }
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);

        replay(myClassMock, MyClass.class);

        tested.invokeVoidMethod();

        verify(myClassMock, MyClass.class);
    }
View Full Code Here

        verifyAll();
    }

    @Test
    public void testNewWithArrayVarArgs() throws Exception {
        ExpectNewDemo tested = new ExpectNewDemo();
        VarArgsConstructorDemo varArgsConstructorDemoMock = createMock(VarArgsConstructorDemo.class);

        final byte[] byteArrayOne = new byte[] { 42 };
        final byte[] byteArrayTwo = new byte[] { 17 };
        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();
    }
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.