下面是我使用自己的匹配器完成的一个测试。我知道在这种情况下我可以使用标准匹配器,但在我的真实代码中我需要一个更复杂的匹配器。
测试通过 - 勾选 VG。问题是似乎有一个额外的IArgumentMatcher.matches()
方法调用返回 false,但测试通过了。
我得到的输出是:
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]
所以问题是为什么我得到了 MISMATCH 行,并且我做错了什么?
测试代码为:
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(");
}
}
}