From: <sea...@ce...> - 2003-08-17 23:34:49
|
Hi all I have just revisited this problem. What I did was to modify the Mock class. I'm not sure if it was the "right" thing to do, but it works for me and it may help someone else. I added these two methods to Mock.java public void expectAndReturn(String methodName, Object singleEqualArg, Callable result) { this.expectAndReturn(methodName, createConstraintMatcher(singleEqualArg), result); } public void expectAndReturn(String methodName, ConstraintMatcher args, Callable stub) { callSequence.addExpect(callFactory.createCallExpectation(callFactory.createCallSignature(methodName, args, stub))); } Now I can do this (using the original example): // Note: final so they can be used by the anonymous inner class final ResultSetHandler resultSetHandler = .... final Mock mockResultSet = new Mock(ResultSet.class); Mock mockSQLExecuter = new Mock(SQLExecuter.class); mockSQLExecuter.expectAndReturn( "execute", C.eq( expectedSQL, expectedBindVariables, resultSetHandler ), (Callable)new ReturnStub() { public Object call(Mock mock, String methodName, Object[] args) throws Throwable { resultSetHandler.handle( (ResultSet)mockResultSet.proxy() ); return super.call( mock, methodName, args ); } } ); What do you think? Sean. >I have just started using Dynamic Mocks in MockObjects v0.09. >On the MockObjects wiki (http://mockobjects.com/wiki/CallableDecorators) >there is a discussion about writing a custom stub when you want to mock a >method that has side-effects. The example given is: >// Note: final so they can be used by the anonymous inner class >final ResultSetHandler resultSetHandler = .... >final Mock mockResultSet = new Mock(ResultSet.class); >Mock mockSQLExecuter = new Mock(SQLExecuter.class); >mockSQLExecuter.add( > new CallOnceExpectation( > new CallSignature( "execute", C.eq( expectedSQL, >expectedBindVariables, resultSetHandler ) ), > new VoidStub() { > public void getDescription() { > return super.getDescription() + " [calls back to >resultSetHandler]"; > } > public Object call( String methodName, Object[] args ) { > resultSetHandler.handle( (ResultSet)mockResultSet.proxy() ); > return super.call( methodName, args ); // stubs the void >result > } > } ) ) ); >I need to do something similar, but this approach does not seem to be >supported. >For example, there is no Mock.add method, or any means to supply a custom >CallStub. >What is the currently recommended approach, or am I missing something? Important: This e-mail is intended for the use of the addressee and may contain information that is confidential, commercially valuable or subject to legal or parliamentary privilege. If you are not the intended recipient you are notified that any review, re-transmission, disclosure, use or dissemination of this communication is strictly prohibited by several Commonwealth Acts of Parliament. If you have received this communication in error please notify the sender immediately and delete all copies of this transmission together with any attachments. |