JUnit Easymock 意外的方法调用

发布于 2024-08-18 22:15:24 字数 722 浏览 6 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

归途 2024-08-25 22:15:24

您还可以使用 EasyMock.isA(OtherObj.class) 来提高类型安全性。

You could also use EasyMock.isA(OtherObj.class) for a little more type safety.

开始看清了 2024-08-25 22:15:24

如果您无法在测试代码中获取对象本身的引用,则可以使用 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 OtherObjwhen declaring the expected method call.

如若梦似彩虹 2024-08-25 22:15:24

如果您只想通过此调用,则 anyObject() 匹配器非常有用,但如果您实际上想验证构造的对象是否是您认为的那样,则可以使用 Capture。它看起来像:

Capture<OtherObj> capturedOtherObj = new Capture<OtherObj>();
mockDao.insert(capture(capturedOtherObj));
replay(mockDao);

objUnderTest.myMethod();

assertThat("captured what you expected", capturedOtherObj.getValue().getId(), 
           equalTo(expectedId));

此外,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:

Capture<OtherObj> capturedOtherObj = new Capture<OtherObj>();
mockDao.insert(capture(capturedOtherObj));
replay(mockDao);

objUnderTest.myMethod();

assertThat("captured what you expected", capturedOtherObj.getValue().getId(), 
           equalTo(expectedId));

Also, PowerMock has the ability to expect an object to be constructed, so you could look into that if you wanted.

无风消散 2024-08-25 22:15:24

另请注意,如果您使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文