Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could use a matcher (<a href="http://code.google.com/p/hamcrest" rel="nofollow noreferrer">http://code.google.com/p/hamcrest</a>) to check whether you get the correct arguments passed into your method. Note you'll need to statically import your matcher (HasCorrelationId in my example).</p> <p>The example below asserts that client.forwardMessage(m) is called once with a message with null correlationId and once with a message with correlationId="abc".</p> <pre><code>import org.hamcrest.Description; import org.hamcrest.TypeSafeMatcher; public class HasCorrelationId extends TypeSafeMatcher&lt;Message&gt; { private final Object expectedCorrelationId; public HasCorrelationId(Object expectedCorrelationId) { this.expectedCorrelationId = expectedCorrelationId; } @Override public boolean matchesSafely(Message message) { if (expectedCorrelationId==message.getCorrelationId()) return true; return expectedCorrelationId==null? false: expectedCorrelationId.equals(message.getCorrelationId()); } public void describeTo(Description description) { description.appendText("hasCorrelationId("+expectedCorrelationId+")"); } public static HasCorrelationId hasCorrelationId(Object correlationId) { return new HasCorrelationId(correlationId); } } </code></pre> <p>... and now for the test:</p> <pre><code>public class MyTest { private Mockery context = new Mockery(); @Test public void testCorrelationId() { final Client client = context.mock(Client.class); final Message message1 = context.mock(Message.class,"message1"); final Message message2 = context.mock(Message.class,"message2"); context.checking(new Expectations(){{ allowing(message1).getCorrelationId();will(returnValue(null)); allowing(message2).getCorrelationId();will(returnValue("abc")); one(client).forwardMessage(with(hasCorrelationId(null))); one(client).forwardMessage(with(hasCorrelationId("abc"))); }}); client.forwardMessage(message1); client.forwardMessage(message2); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload