easymock - 匹配器和多次调用
下面是我完成的一个使用它自己的匹配器的测试。我知道在这种情况下我可以使用标准匹配器,但在我的真实代码中我需要一个更复杂的匹配器。
测试通过 - 勾选 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(");
}
}
}
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.
The test passes - tick VG.
The issue is that there appears to be an extra call to IArgumentMatcher.matches()
method that returns false, and yet the test passes.
The output I get is:
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]
So the question is why am I getting the MISMATCH line,
and have I done something incorrectly?
The test code is:
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(");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
快速播放,看起来您没有定义顺序,只是应该期待一些事情。每次调用模拟时,它都会按顺序遍历每个期望,直到找到匹配且之前未调用过的期望。
所以:
正如您所期望的。首场比赛命中。
按顺序排列每个(包括已经通过的),直到找到匹配项。
每个按顺序排列,直到找到之前未调用过的命中。
这将使其能够在“意外的方法调用,预期 1,实际 1 (+1)”行上提供错误消息。
--
注释代码 -
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:
Just as you would expect. First match hits.
Each one in order (including ones that have already passed) until it finds a hit.
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 -