24 January 2006

jMock ordering

In case anyone (i.e. me next week) needs to specify explicit method ordering in jMock, here it is. This code specifies the order of a chain of method calls (it came from testing a wire protocol implementation - don't ask...), rather than just expecting all calls to come after a single call. In particular note the ordering of the id() and after() calls, once again IntelliJ saves the day.

Mock mock = mock(RedstripeSocketTransport.class);
mock.expects(once()).method(METHOD_OPEN).id("CallOpen");
mock.expects(once()).method(METHOD_WRITE).with(eq(seededRequest))
    .after("CallOpen").will(returnValue("foo")).id("CallWrite");  
mock.expects(once()).method(METHOD_READ).after("CallWrite")
    .will(returnValue(seededResponse)).id("CallRead");
mock.expects(once()).method(METHOD_CLOSE).after("CallRead");
return (RedstripeSocketTransport) mock.proxy();

Note that that write() does not actually return a value in the production code, it's in the post to help me remember ;) Apologies to Andrew if he's posted this already, I thought he had, but google does not know about it...

Vlad has made a good suggestion in the comments, that statements have the method name given to them as ID by default. I'd seen this as an error message but wasn't sure what it meant.

4 Comments:

At 24/1/06 2:01 pm, Blogger Vlad said...

Hi Tom,

Another hint: you don't have to give the IDs to the statements. Each statement has a default id equal to the name of the method invoked, e.g. statement mock.expects(once()).method(METHOD_OPEN).id("CallOpen"); has a default ID of "open".

Hope this helps,

Vlad

 
At 24/1/06 8:41 pm, Blogger Andrew said...

Yes, Tom, yes I did:

http://morenews.blogspot.com/2005/04/jmock-invocation-order.html.

And of course Vlad is correct, you do get one name for free.

 
At 25/1/06 7:54 am, Blogger Tom Adams said...

Vladley, I was getting an error when I exlicitly named each statement the same as the method, now I know why! Cheers!

 
At 25/1/06 7:56 am, Blogger Tom Adams said...

Damn Google, can't find anything... I'll just ask you next time ;)

 

Post a Comment

<< Home