The Callable interface and the decorator/stub structure is designed to
achieve exactly what you need. If you want to mock a side-effect, create a
decorator chain with a domain-specific stub that implements the side effect.
It is easy to write the stub as an anonymous inner class in the body of the
test, and then later extract it into a reusable class if you need it in more
than one test.
For example, in long-hand using the version 0.09 API:
// Note: resultSetHandler and resultSet are 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() { " [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
}
} ) ) );
In my experimental branch I have added an expect method to my branch that
takes three parameters: a method name, argument constraints and a Callable
stub. This creates the CallOnceExpectation, NameMatcher, ArgumentMatcher
around the stub, and so makes it more convenient to use domain-specific
stubs in tests.
I have also put some documentation about these interfaces/classes on the
Mock Objects wiki, so check that out for more info.
Regards,
Nat.
_______________________
Dr. Nathaniel Pryce
B13media Ltd.
http://www.b13media.com
+44 (0)7712 526 661
----- Original Message -----
From: <Ext...@no...>
To: <moc...@li...>
Sent: Wednesday, July 02, 2003 3:25 PM
Subject: [MO-java-users] Modifying method parameters with DynaMock
Hi,
I want to "do something" with a parameter that is passed into a method that
I am DynaMock'ing. The interface is:
public interface SqlExecutor {
public void execute(String sql, Iterator bindVariables, ResultSetHandler
handler) throws SQLException;
}
and
public interface ResultSetHandler {
public void handle(ResultSet resultSet) throws SQLException;
}
So, I create a DynaMock of SqlExecutor, my code under test passes in some
implementation of ResultSetHandler, and I need the DynaMock to push a
ResultSet into this ResultSetHandler implementation.
Can DynaMock do this, or should I fall back on plain old expectation classes
etc?
Thanks,
Mike.
-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100006ave/direct;at.asp_061203_01/01
_______________________________________________
Mockobjects-java-users mailing list
Moc...@li...
https://lists.sourceforge.net/lists/listinfo/mockobjects-java-users
|