From: Nat P. <np...@us...> - 2002-10-31 17:03:50
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic In directory usw-pr-cvs1:/tmp/cvs-serv16430/src/core/test/mockobjects/dynamic Modified Files: MockTest.java Added Files: CallCounterTest.java Removed Files: MockTrainerTest.java Log Message: Removed Trainer while refactoring Mock class Extracted MockCall interface from ExpectedCall abstract class Moved argument checking into ExpectedCall Changed uses of ExpectedCall to uses of MockCall Renamed ExpectedCall etc. to MockCall etc. Added CallCounter to decorator to show advantages of MockCall interface Cleaned up some javadoc --- NEW FILE: CallCounterTest.java --- /** Created on Oct 31, 2002 by npryce * Copyright (c) B13media Ltd. */ package test.mockobjects.dynamic; import junit.framework.AssertionFailedError; import com.mockobjects.dynamic.CallCounter; import com.mockobjects.dynamic.Mock; import com.mockobjects.dynamic.MockCall; import com.mockobjects.dynamic.P; import com.mockobjects.util.TestCaseMo; public class CallCounterTest extends TestCaseMo { public CallCounterTest( String test ) { super(test); } public void testCallCounterPassesArguments() throws Throwable { Mock mock_call = new Mock(); CallCounter counter = new CallCounter( 1, (MockCall)mock_call.createInterface(MockCall.class) ); final Object[] ARGS = { new Object() }; mock_call.expectVoid( "call", P.args( P.same(ARGS) ) ); counter.call( ARGS ); mock_call.verify(); counter.verify(); } public void testCallCounterReturnsValue() throws Throwable { Mock mock_call = new Mock(); CallCounter counter = new CallCounter( 1, (MockCall)mock_call.createInterface(MockCall.class) ); final Object RESULT = new Object(); mock_call.expectReturn( "call", P.ANY_ARGS, RESULT ); assertSame( RESULT, counter.call( new Object[0] ) ); mock_call.verify(); counter.verify(); } public void testCallCounterThrowsException() throws Throwable { Mock mock_call = new Mock(); CallCounter counter = new CallCounter( 1, (MockCall)mock_call.createInterface(MockCall.class) ); final Object RESULT = new Object(); mock_call.expectThrow( "call", P.ANY_ARGS, new ExampleException() ); try { counter.call( new Object[0] ); } catch( ExampleException ex ) { // expected } mock_call.verify(); counter.verify(); } public void testTooManyCalls() throws Throwable { Mock mock_call = new Mock(); final CallCounter counter = new CallCounter( 2, (MockCall)mock_call.createInterface(MockCall.class) ); counter.call( new Object[0] ); counter.call( new Object[0] ); try { counter.call( new Object[0] ); } catch( AssertionFailedError expected ) { return; } fail( "third call should have failed" ); } public void testTooFewCalls() throws Throwable { Mock mock_call = new Mock(); final CallCounter counter = new CallCounter( 2, (MockCall)mock_call.createInterface(MockCall.class) ); counter.call( new Object[0] ); try { counter.verify(); } catch( AssertionFailedError expected ) { return; } fail( "verify should have failed because of too few calls" ); } class ExampleException extends Exception { } } Index: MockTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/MockTest.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- MockTest.java 25 Oct 2002 22:07:48 -0000 1.1 +++ MockTest.java 31 Oct 2002 17:03:46 -0000 1.2 @@ -40,10 +40,6 @@ public static class DerivedMock extends Mock { - DerivedMock( String name ) { - super(name); - } - public boolean was_called = false; public void voidMethod() { @@ -82,11 +78,6 @@ _mock.verify(); } - public void testNamedMock() { - Mock mock = new Mock("Name"); - assertEquals( "Name", mock.toString() ); - } - public void testMockCallToNoArgMethod() { Object result = new Object(); @@ -155,21 +146,36 @@ public void testExpectCallWithSideEffect() { final StringBuffer buf = new StringBuffer(); - _mock.expect( - new ExpectedCall( "sideEffect", - new Predicate[]{ new IsSame(buf) } ) - { - public Object eval( Object[] args ) throws Throwable { - buf.append("hello"); - return Mock.VOID; - } - } ); + _mock.expect( "sideEffect", new MockCall() { + public Object call( Object[] args ) throws Throwable { + assertSame( "wrong buffer", buf, args[0] ); + buf.append("hello"); + return Mock.VOID; + } + } ); _interface.sideEffect( buf ); assertEquals( "hello", buf.toString() ); _mock.verify(); } + public void testFailingExpectationInCallWithSideEffect() { + final StringBuffer buf = new StringBuffer(); + _mock.expect( "sideEffect", new MockCall() { + public Object call( Object[] args ) throws Throwable { + assertSame( "wrong buffer", buf, args[0] ); + buf.append("hello"); + return Mock.VOID; + } + } ); + + assertFails( "should fail with wrong buffer", new Runnable() { + public void run() { + _interface.sideEffect( new StringBuffer() ); + } + } ); + } + public void testNotAllMethodsCalled() { _mock.expectVoid("noArgs", Mock.NO_ARGS); @@ -357,8 +363,8 @@ public void testSetupCallWithSideEffect() { final StringBuffer buf = new StringBuffer(); - _mock.setup( new ExpectedCall("sideEffect") { - public Object eval( Object[] args ) throws Throwable { + _mock.setup( "sideEffect", new MockCall() { + public Object call( Object[] args ) throws Throwable { buf.append("hello"); return Mock.VOID; } @@ -380,8 +386,8 @@ } public void testSetupCallWithSideEffectDoesNotRequireCall() { - _mock.setup( new ExpectedCall("sideEffect") { - public Object eval( Object[] args ) throws Throwable { + _mock.setup( "sideEffect", new MockVoidCall() { + public Object call( Object[] args ) throws Throwable { throw new RuntimeException("should not be called"); } } ); @@ -410,9 +416,9 @@ } fail("should have thrown AssertionFailedError when return from void method"); } - + public void testDerivedMockClass() { - DerivedMock mock = new DerivedMock("Derived"); + DerivedMock mock = new DerivedMock(); ExampleInterface i = (ExampleInterface)Proxy.newProxyInstance( getClass().getClassLoader(), new Class[]{ ExampleInterface.class }, @@ -426,7 +432,7 @@ } public void testDerivedClassPassingInvocationToMockCall() { - DerivedMock mock = new DerivedMock("Derived"); + DerivedMock mock = new DerivedMock(); ExampleInterface i = (ExampleInterface)Proxy.newProxyInstance( getClass().getClassLoader(), new Class[]{ ExampleInterface.class }, --- MockTrainerTest.java DELETED --- |