Note that there are some explanatory texts on larger screens.

plurals
  1. POeasymock - matcher and multiple calls
    text
    copied!<p>Below is a test I have knocked up that uses its own matcher. I know in this case I could use a standard matcher but in my real code I need a more complicated one.</p> <p>The test passes - tick VG. The issue is that there appears to be an extra call to <code>IArgumentMatcher.matches()</code> method that returns false, and yet the test passes.</p> <p>The output I get is:</p> <pre><code>30-09-2009 16:12:23 [main] ERROR - MATCH - expected[aa], actual[aa] 30-09-2009 16:12:23 [main] ERROR - MISMATCH - expected[aa], actual[bb] 30-09-2009 16:12:23 [main] ERROR - MATCH - expected[bb], actual[bb] </code></pre> <p>So the question is why am I getting the MISMATCH line, and have I done something incorrectly?</p> <p>The test code is:</p> <pre><code>package uk.co.foo; import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.reset; import static org.easymock.EasyMock.verify; import junit.framework.TestCase; import org.apache.log4j.Logger; import org.easymock.EasyMock; import org.easymock.IArgumentMatcher; /** * */ public class BillTest extends TestCase { private static Logger mLogger = Logger.getLogger(BillTest.class); private BillInterface mMockBill; public void testTwoCalls() throws Exception { BillsTestClass sut = new BillsTestClass(); sut.setDao(mMockBill); expect(mMockBill.method1(eqBillMatcher("aa"))).andReturn(""); expect(mMockBill.method1(eqBillMatcher("bb"))).andReturn(""); replay(mMockBill); //test method sut.doSomething("aa"); sut.doSomething("bb"); verify(mMockBill); } public String eqBillMatcher(String aIn) { EasyMock.reportMatcher(new BillMatcher(aIn)); return null; } @Override protected void setUp() throws Exception { super.setUp(); mMockBill = createMock(BillInterface.class); } @Override protected void tearDown() throws Exception { super.tearDown(); reset(mMockBill); } public class BillsTestClass { private BillInterface mDao; public void setDao(BillInterface aDao) { mDao = aDao; } public void doSomething(String aValue) { mDao.method1(aValue); } } public interface BillInterface { String method1(String aValue); } public class BillMatcher implements IArgumentMatcher { private String mExpected; public BillMatcher(String aExpected) { mExpected = aExpected; } /** * @see org.easymock.IArgumentMatcher#matches(java.lang.Object) * {@inheritDoc} */ public boolean matches(Object aActual) { if (aActual.equals(mExpected)) { mLogger.error("MATCH - expected[" + mExpected + "], actual[" + aActual + "]"); return true; } mLogger.error("MISMATCH - expected[" + mExpected + "], actual[" + aActual + "]"); return false; } /** * @see org.easymock.IArgumentMatcher#appendTo(java.lang.StringBuffer) * {@inheritDoc} */ public void appendTo(StringBuffer aBuffer) { aBuffer.append("boo("); } } } </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