tags:

views:

937

answers:

1
A: 

A quick play and it looks like you do not define order just that something should be expected. Each time you make a call to the mock it runs through every expectation in order until it finds one that matches and has not been called before.

So with:

expect(mMockBill.method1(eqBillMatcher("aa"))).andReturn("");
expect(mMockBill.method1(eqBillMatcher("aa"))).andReturn("");
expect(mMockBill.method1(eqBillMatcher("cc"))).andReturn("");

sut.doSomething("aa");

MATCH - expected[aa], actual[aa]

Just as you would expect. First match hits.

sut.doSomething("cc");

MISMATCH - expected[aa], actual[cc]
MISMATCH - expected[aa], actual[cc]
MATCH - expected[cc], actual[cc]

Each one in order (including ones that have already passed) until it finds a hit.

sut.doSomething("aa");

MATCH - expected[aa], actual[aa]
MATCH - expected[aa], actual[aa]

Each in order until it finds a hit that has not been called before.

This would enable it to provide the error messages on the lines of "unexpected method call, expected 1, actual 1 (+1).

--

Code for comment -

private boolean used = false;

<snip/>

public boolean matches(Object aActual) {
   if (used) {
     return false;
   }
   used = true;
mlk
I have changed it to use createStrictMock() as I do want to define the order but I was not expecting to get these MISMATCH lines. The objects I am comparing are quite complex so that is why I have the logger lines in the Matcher. It is annoying that I see these even when the test passes.
Bill Comer
sorry to be clearer - I only want to see my message in the event of a test failure. Any idea if that is possible ?
Bill Comer
Looking at the way it is implemented (org.easymock.internal.ExpectedInvocation.matches) - No I don't believe you can. You could make BillMatcher smarter (say always return false after it has returned true) but this might confuse EasyMock. (see updated message)
mlk