从 EasyMock 的“nice mock”中获取异常附带调试器
(免责声明 - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
补充杰夫的答案。
来自 EasyMock 的方法 createNiceMock javadoc:
通过此方法创建的模拟对象不需要任何配置(预期的调用)。您只需创建它并“重播它”。示例:
在创建的存根上允许任何方法调用(它不会抛出异常),并且它们将始终返回默认值(0、null 或 false)。如果您设置了特定的调用期望,那么您必须配置它的返回值,否则您将收到错误(这就是您的情况)。
如果您想限制可以执行哪些方法(如果调用意外方法,则使测试失败),恐怕您必须创建一个常规模拟,设置每个调用期望以及每个方法的返回值那些。
Complementing on Jeff's answer.
From EasyMock's method createNiceMock javadoc:
A mock object created by this method don't need any configuration (expected invocations). You just have to create it and "replay it". Example:
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.
如果您的翻译方法返回一个值,您需要为其设置一个期望。
If your translate method returns a value, you need to setup an expectation for it.
您需要调用
EasyMock.replay(foo)
。在执行此操作之前,您的模拟对象处于“记录状态”。来自 EasyMock 文档:如果您要创建存根对象,只需调用
createNiceMock
,然后调用replay
:You need to call
EasyMock.replay(foo)
. Before you do that your mock object is in a "record state". From EasyMock documentation:If you with to create a stub object just call
createNiceMock
followed byreplay
: