从EasyMock的“不错的模拟"中获取异常信息附带调试器 [英] Getting exception from EasyMock's "nice mock" with a debugger attached

查看:107
本文介绍了从EasyMock的“不错的模拟"中获取异常信息附带调试器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(免责声明-EasyMock newb)

(Disclaimer - EasyMock newb)

根据文档(和此帖子),如果我愿意的话要使用EasyMock生成存根对象,我应该使用EasyMock.createNiceMock(). 好模拟"实际上是一个存根-即不参与验证的对象,仅返回值.

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.

但是,以下代码段对我来说在第二行foo.translate()行上以IllegalStateException("missing behavior definition for the preceding method")失败.

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

任何人都可以解释一下,或者告诉我如何使用EasyMock创建零冗长(o(number_of_exercised_mock_methods))的存根.

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?

推荐答案

对Jeff的答案进行了补充.

Complementing on Jeff's answer.

通过EasyMock的方法createNiceMock javadoc:

From EasyMock's method createNiceMock javadoc:

创建一个实现给定接口的模拟对象,进行顺序检查 默认情况下处于禁用状态,并且模拟对象将返回0, 对于意外调用,为null或false.

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);

在创建的存根上允许任何方法调用(它不会引发Exception),并且它们将始终返回默认值(0,null或false).如果您设置了特定的调用期望,则必须配置它的返回值,否则会得到一个错误(这是您的情况).

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.

这篇关于从EasyMock的“不错的模拟"中获取异常信息附带调试器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆