JUnit Easymock 意外的方法调用
我正在尝试使用 EasyMock 在 JUnit 中设置一个测试,但遇到了一个我似乎无法理解的小问题。我希望这里有人能提供帮助。
这是我尝试测试的方法的简化版本:
public void myMethod() {
//(...)
Obj myObj = this.service.getObj(param);
if (myObj.getExtId() != null) {
OtherObj otherObj = new OtherObj();
otherObj.setId(myObj.getExtId());
this.dao.insert(otherObj);
}
//(...)
}
好的,我使用 EasyMock 模拟了 service.getObj(myObj)
调用,效果很好。
当 JUnit 调用 dao.insert(otherObj) 调用时,我的问题就出现了。 EasyMock 会抛出一个*意外的方法调用*
。
我不介意在测试中嘲笑该 dao 并在其上使用 expectLastCall().once();
,但这假设我有一个作为参数传递的“otherObj”的句柄插入时间... 我当然不这样做,因为它是在正在测试的方法的上下文中有条件地创建的。
有人曾经遇到过这个问题并以某种方式解决过吗?
谢谢。
I'm trying to setup a test in JUnit w/ EasyMock and I'm running into a small issue that I can't seem to wrap my head around. I was hoping someone here could help.
Here is a simplified version of the method I'm trying to test:
public void myMethod() {
//(...)
Obj myObj = this.service.getObj(param);
if (myObj.getExtId() != null) {
OtherObj otherObj = new OtherObj();
otherObj.setId(myObj.getExtId());
this.dao.insert(otherObj);
}
//(...)
}
Ok so using EasyMock I've mocked the service.getObj(myObj)
call and that works fine.
My problem comes when JUnit hits the dao.insert(otherObj
) call. EasyMock throws a *Unexpected Method Call*
on it.
I wouldn't mind mocking that dao in my test and using expectLastCall().once();
on it, but that assumes that I have a handle on the "otherObj" that's passed as a parameter at insert time...
Which of course I don't since it's conditionally created within the context of the method being tested.
Anyone has ever had to deal with that and somehow solved it?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您还可以使用 EasyMock.isA(OtherObj.class) 来提高类型安全性。
You could also use
EasyMock.isA(OtherObj.class)
for a little more type safety.如果您无法在测试代码中获取对象本身的引用,则可以使用
EasyMock.anyObject()
作为insert
方法的预期参数。顾名思义,它期望用......好吧,任何对象来调用该方法:)这可能比匹配确切的参数不太严格,但如果您对此感到满意,请尝试一下。请记住在声明预期的方法调用时包含对
OtherObj
的强制转换。If you can't get a reference to the object itself in your test code, you could use
EasyMock.anyObject()
as the expected argument to yourinsert
method. As the name suggests, it will expect the method to be called with.. well, any object :)It's maybe a little less rigorous than matching the exact argument, but if you're happy with it, give it a spin. Remember to include the cast to
OtherObj
when declaring the expected method call.如果您只想通过此调用,则 anyObject() 匹配器非常有用,但如果您实际上想验证构造的对象是否是您认为的那样,则可以使用 Capture。它看起来像:
此外,PowerMock 能够期望构造一个对象,所以如果你愿意的话你可以研究一下。
The anyObject() matcher works great if you just want to get past this call, but if you actually want to validate the constructed object is what you thought it was going to be, you can use a Capture. It would look something like:
Also, PowerMock has the ability to expect an object to be constructed, so you could look into that if you wanted.
另请注意,如果您使用 EasyMock.createStrictMock();,方法调用的顺序也很重要,如果您违反此规则,则会引发意外的方法调用。
Note also that if you use
EasyMock.createStrictMock();
, the order of the method calls is also important and if you break this rule, it would throw an unexpected method call.