从 EasyMock 的“nice mock”中获取异常附带调试器

发布于 2024-11-10 15:09:09 字数 759 浏览 2 评论 0原文

(免责声明 - EasyMock newb)

根据文档(和这篇文章) ,如果我想使用 EasyMock 生成存根对象,我应该使用 EasyMock.createNiceMock()。一个“好的模拟”实际上是一个存根——即一个不参与验证、只返回值的对象。

但是,以下代码片段对我来说失败,并在第二个 foo.translate() 行上出现 IllegalStateException("missing behaviorDefinition for the previous method")

Foo foo = EasyMock.createNiceMock(Foo.class);
EasyMock.replay(foo); // added this line
foo.translate("a", "b");
foo.translate("a", "b"); // only the second calls throws an exception

谁能解释一下这一点,或者告诉我如何使用 EasyMock 创建零详细的存根(o(number_of_exercished_mock_methods))。

编辑 - 我注意到,在附加调试器时几乎总是会遇到这些错误,但在未附加调试器时绝不会出现这些错误。知道这有什么关系吗?

(Disclaimer - EasyMock newb)

According to the documentation (and this post), if I wanted to use EasyMock to generate stub objects, I should use EasyMock.createNiceMock(). A "nice mock" is actually a stub - i.e an object that doesn't participate in validation, just returns values.

However, the following snippet fails for me with an IllegalStateException("missing behavior definition for the preceding method"), on the second foo.translate() line.

Foo foo = EasyMock.createNiceMock(Foo.class);
EasyMock.replay(foo); // added this line
foo.translate("a", "b");
foo.translate("a", "b"); // only the second calls throws an exception

Can anyone explain this, or rather tell me how to use EasyMock to create stubs with zero verbosity (o(number_of_exercised_mock_methods)).

Edit - I've noticed that I'm getting these errors almost always when a debugger is attached, but never when it isn't attached. Any idea how that may be related?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

盛装女皇 2024-11-17 15:09:09

补充杰夫的答案。

来自 EasyMock 的方法 createNiceMock javadoc:

创建一个实现给定接口的模拟对象,顺序检查
默认禁用,模拟对象将返回0
null 或 false 表示意外调用。

通过此方法创建的模拟对象不需要任何配置(预期的调用)。您只需创建它并“重播它”。示例:

ComplicatedObject stub = EasyMock.createNiceMock();
replay(stub);

在创建的存根上允许任何方法调用(它不会抛出异常),并且它们将始终返回默认值(0、null 或 false)。如果您设置了特定的调用期望,那么您必须配置它的返回值,否则您将收到错误(这就是您的情况)。

如果您想限制可以执行哪些方法(如果调用意外方法,则使测试失败),恐怕您必须创建一个常规模拟,设置每个调用期望以及每个方法的返回值那些。

Complementing on Jeff's answer.

From EasyMock's method createNiceMock javadoc:

Creates a mock object that implements the given interface, order checking
is disabled by default, and the mock object will return 0,
null or false for unexpected invocations.

A mock object created by this method don't need any configuration (expected invocations). You just have to create it and "replay it". Example:

ComplicatedObject stub = EasyMock.createNiceMock();
replay(stub);

Any method call is allowed on the created stub (it won't throw an Exception), and they will always return the default value (0, null or false). If you set up an specific invocation expectation, then you'll have to configure it's return value or you'll get an error (that's your case).

If you want to restrict which methods can be executed (making the test fail if an unexpected method is called), them I'm afraid you'll have to create a regular mock, set up every invocation expectation and a return value for each of those.

枫以 2024-11-17 15:09:09

如果您的翻译方法返回一个值,您需要为其设置一个期望。

expect(foo.translate("a","b")).andStubReturn(retVal);

If your translate method returns a value, you need to setup an expectation for it.

expect(foo.translate("a","b")).andStubReturn(retVal);
伪心 2024-11-17 15:09:09

您需要调用EasyMock.replay(foo)。在执行此操作之前,您的模拟对象处于“记录状态”。来自 EasyMock 文档:

处于录音状态(调用前
重播),模拟对象不
表现得像一个模拟对象,但它
记录方法调用。打电话后
重播,它的行为就像一个模拟对象,
检查是否是预期的方法
通话确实完成了。

如果您要创建存根对象,只需调用createNiceMock,然后调用replay

Foo foo = EasyMock.createNiceMock(Foo.class);
EasyMock.replay(foo);
foo.translate("a", "b");
foo.translate("a", "b");

You need to call EasyMock.replay(foo). Before you do that your mock object is in a "record state". From EasyMock documentation:

In the record state (before calling
replay), the Mock Object does not
behave like a Mock Object, but it
records method calls. After calling
replay, it behaves like a Mock Object,
checking whether the expected method
calls are really done.

If you with to create a stub object just call createNiceMock followed by replay:

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