Java Mailing List Archive

http://www.gg3721.com/

Home » user.jmock »

[jmock-user] How to check object state inside a mock call

Dimitry Polivaev

2012-09-05

Replies: Find Java Web Hosting

Author LoginPost Reply
Hi,

I am trying to develop a class performing callbacks using JMock.
I have to check some properties of the calling object of the tested class RuleEngine during the callback.
After the call finishes its state is lost.

So I wrote the following:

@RunWith(JMock.class)
public class RuleEngineTest {
Mockery context = new JUnit4Mockery();
Code code = context.mock(Code.class);
@Test
public void emptyRuleSetRunsCodeOnce() {
final RuleEngine ruleEngine = new RuleEngine();
context.checking(new Expectations() {{
oneOf(code).run();
will(new CheckingAction(){
@Override
protected void doChecks() {
// Do some state checks of ruleEngine object
}});
}});

ruleEngine.run(code);
}
}

public abstract class CheckingAction extends ReturnDefaultValueAction{

@Override
public Object invoke(Invocation invocation) throws Throwable {
doChecks();
return super.invoke(invocation);
}

abstract protected void doChecks();
}

// class to be developed. The internal state and the related methods come later.
public class RuleEngine {
public void run(Code code) {
code.run();
}
}


However writing the checking part as
will(new CheckingAction(){
   @(protected)}
});

does not seem to be right because I actually misuse Action interface to perform some checks and have some syntax overhead.

Is there a better way?

Kind regards, Dimitry

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email


©2008 gg3721.com - Jax Systems, LLC, U.S.A.