From: Tim M. <ma...@us...> - 2003-04-16 17:20:39
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv27819/core/test/mockobjects/dynamic Modified Files: Tag: DynamicMockExperiment CallSequenceTest.java Log Message: Missing test for call sequence index checking. Index: CallSequenceTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/Attic/CallSequenceTest.java,v retrieving revision 1.5.2.1 retrieving revision 1.5.2.2 diff -u -r1.5.2.1 -r1.5.2.2 --- CallSequenceTest.java 14 Apr 2003 18:46:55 -0000 1.5.2.1 +++ CallSequenceTest.java 16 Apr 2003 17:20:32 -0000 1.5.2.2 @@ -3,122 +3,147 @@ */ package test.mockobjects.dynamic; -import junit.framework.AssertionFailedError; -import junit.framework.TestCase; - import com.mockobjects.constraint.Constraint; + import com.mockobjects.dynamic.C; import com.mockobjects.dynamic.CallSequence; import com.mockobjects.dynamic.Mock; + import com.mockobjects.util.AssertMo; +import junit.framework.AssertionFailedError; +import junit.framework.TestCase; + + public class CallSequenceTest extends TestCase { + private static final String MOCK_NAME = "Test mock"; final String METHOD_A_NAME = "methodA"; final String METHOD_A_RESULT = "resultA"; final String METHOD_B_NAME = "methodB"; - final String METHOD_B_RESULT = "resultB"; - private static final String MOCK_NAME = "Test mock"; - + final String METHOD_B_RESULT = "resultB"; final Throwable METHOD_A_EXCEPTION = new DummyThrowable("Configured test throwable"); final String[] METHOD_A_ARGS = new String[] { "a1", "a2" }; final Constraint[] METHOD_A_CONSTRAINTS = C.args(C.eq("a1"), C.eq("a2")); final String[] METHOD_B_ARGS = new String[] { "b1", "b2" }; private CallSequence callSequence = new CallSequence(); private Mock unusedMock = null; - private MockCallable methodA = new MockCallable(); private MockCallable methodB = new MockCallable(); private MockCallable mockCallable = new MockCallable(); - - public void testCallableThrowableThrown() - throws Throwable { + + public CallSequenceTest(String name) { + super(name); + } + + public void testCallableThrowableThrown() throws Throwable { final Throwable throwable = new DummyThrowable(); - + mockCallable.setupMatchesReturn(true); mockCallable.setupCallThrow(throwable); - + callSequence.add(mockCallable); - + try { callSequence.call(unusedMock, "hello", new String[0]); } catch (Throwable ex) { assertSame("exception is caught by mock", throwable, ex); } } + public void testCallFailsOnEmptyList() throws Throwable { try { callSequence.call(unusedMock, "missingMethod", new Object[0]); } catch (AssertionFailedError ex) { AssertMo.assertIncludes("reports empty set in error message", "no methods", ex.getMessage()); - + return; } - + fail("Should fail for a missing item"); } + + public void testCallFailsWithTooManyCalls() throws Throwable { + mockCallable.setupMatchesReturn(true); + mockCallable.setupCallReturn(METHOD_A_RESULT); + callSequence.add(mockCallable); + callSequence.call(unusedMock, "willdefinitelyMatch", new Object[0]); + + try { + callSequence.call(unusedMock, "oneMethodTooMany", new Object[0]); + } catch (AssertionFailedError ex) { + AssertMo.assertIncludes("reports one method call too many", "too many", ex.getMessage()); + + return; + } + + fail("Should fail for calling too many times"); + } + public void testCallPassedToContainedElements() throws Throwable { methodA.setExpectedMatches(METHOD_A_NAME, METHOD_A_ARGS); methodA.setupMatchesReturn(true); methodA.setExpectedCall(unusedMock, METHOD_A_NAME, METHOD_A_ARGS); methodA.setupCallReturn(METHOD_A_RESULT); - + methodB.setExpectedCallCount(0); - + callSequence.add(methodA); callSequence.add(methodB); - - assertSame("expected result from method A", METHOD_A_RESULT, - callSequence.call(unusedMock, METHOD_A_NAME, METHOD_A_ARGS)); - + + assertSame("expected result from method A", METHOD_A_RESULT, callSequence.call(unusedMock, METHOD_A_NAME, METHOD_A_ARGS)); + methodA.verifyExpectations(); methodB.verifyExpectations(); } + public void testCallPassedToContainedElementsOtherOrderShouldFail() throws Throwable { methodA.setupMatchesReturn(false); - + methodA.setupGetDescription("***methodA-description****"); methodB.setupGetDescription("***methodB-description****"); - + callSequence.add(methodA); callSequence.add(methodB); - + try { - assertSame("expected result from method B", METHOD_B_RESULT, - callSequence.call(unusedMock, METHOD_B_NAME, METHOD_B_ARGS)); - } - catch (AssertionFailedError ex) { + assertSame("expected result from method B", METHOD_B_RESULT, callSequence.call(unusedMock, METHOD_B_NAME, METHOD_B_ARGS)); + } catch (AssertionFailedError ex) { String message = ex.getMessage(); AssertMo.assertIncludes("Should have expected error message", "Unexpected call to methodB", message); AssertMo.assertIncludes("Should have arguments in error message (1)", METHOD_B_ARGS[0], message); AssertMo.assertIncludes("Should have arguments in error message (2)", METHOD_B_ARGS[1], message); AssertMo.assertIncludes("Should have Index pointer next to correct item", methodA.getDescription() + " <<<", message); - + AssertMo.assertIncludes("shows set contents (A)", methodA.getDescription(), message); AssertMo.assertIncludes("shows set contents (B)", methodB.getDescription(), message); AssertMo.assertTrue("Method A should be before method B", message.indexOf(methodA.getDescription()) < message.indexOf(methodB.getDescription())); + return; } - + fail("Should fail due to wrong order"); } + public void testConfiguredResultReturned() throws Throwable { final String result = "result"; - + mockCallable.setupCallReturn(result); mockCallable.setupMatchesReturn(true); - + callSequence.add(mockCallable); - + assertSame("result is returned by mock", result, callSequence.call(unusedMock, "method", new Object[0])); } + public void testEmptySetVerifies() throws Exception { callSequence.verify(); } + public void testFailureIfNoElementMatches() throws Throwable { final String methodCName = "methodC"; final String[] methodCArgs = { "c1", "c2" }; - + methodA.setExpectedMatches(methodCName, methodCArgs); methodA.setupMatchesReturn(false); methodA.setExpectedCallCount(0); @@ -127,56 +152,53 @@ methodB.setupMatchesReturn(false); methodB.setExpectedCallCount(0); methodB.setupGetDescription("***methodB-description****"); - + callSequence.add(methodA); callSequence.add(methodB); - + try { callSequence.call(unusedMock, methodCName, methodCArgs); } catch (AssertionFailedError ex) { AssertMo.assertIncludes("method name is in error message", "methodC", ex.getMessage()); AssertMo.assertIncludes("argument is in error message (1)", methodCArgs[0], ex.getMessage()); AssertMo.assertIncludes("argument is in error message (2)", methodCArgs[1], ex.getMessage()); - + AssertMo.assertIncludes("shows set contents (A)", methodA.getDescription(), ex.getMessage()); AssertMo.assertIncludes("shows set contents (B)", methodB.getDescription(), ex.getMessage()); - + return; } - + fail("Should fail for a missing item"); } + public void testVerifiesIfAllContainedElementsVerify() throws Throwable { methodA.setExpectedVerifyCalls(1); methodB.setExpectedVerifyCalls(1); - + callSequence.add(methodA); callSequence.add(methodB); callSequence.verify(); - + methodA.verifyExpectations(); methodB.verifyExpectations(); } + public void testVerifyFailsIfContainedElementDoesNotVerify() throws Exception { methodA.setExpectedVerifyCalls(1); methodA.setupVerifyThrow(new AssertionFailedError("verify failed")); callSequence.add(methodA); - + try { callSequence.verify(); } catch (AssertionFailedError ex) { methodA.verifyExpectations(); - + return; } - - fail("Should have got a failure for contained element failing"); - } - public CallSequenceTest(String name) { - super(name); + fail("Should have got a failure for contained element failing"); } - } |