在非常简单的示例中使用 EasyMock.expect() 时出现编译错误?
我正在尝试使用 EasyMock 一个非常简单的示例,但是我根本无法构建它。我有以下测试用例:
@Test
public void testSomething()
{
SomeInterface mock = EasyMock.createMock(SomeInterface.class);
SomeBase expected = new DerivesFromSomeBase();
EasyMock.expect(mock.send(expected));
}
但是我在 EasyMock.expect(...
行中收到以下错误:
The method expect(T) in the type EasyMock is not applicable for the arguments (void)
有人能指出我正确的方向吗?我完全迷失了。
I am trying a very simple example using EasyMock, however I simply cannot make it build. I have the following test case:
@Test
public void testSomething()
{
SomeInterface mock = EasyMock.createMock(SomeInterface.class);
SomeBase expected = new DerivesFromSomeBase();
EasyMock.expect(mock.send(expected));
}
However I get the following error in the EasyMock.expect(...
line:
The method expect(T) in the type EasyMock is not applicable for the arguments (void)
Can somebody point me in the correct direction? I am completely lost.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想测试
void
方法,请调用您想要在模拟上测试的方法。然后调用expectLastCall()
方法。
这是一个例子:
If you want to test
void
methods, call the method you want to test on your mock. Then call theexpectLastCall()
method.Here's an example:
由于您的 send() 方法返回 void,因此只需使用预期值调用模拟方法并重播即可:
Since your send() method returns void, just call the mock method with expected values and replay:
由于您正在模拟接口,因此模拟方法的唯一目的是从该方法返回结果。在这种情况下,您的“发送”方法的返回类型似乎为 void。 “EasyMock.expect”方法是通用的,需要一个返回类型,这会导致编译器告诉您不能使用 void 方法,因为它没有返回类型。
有关更多信息,请参阅 EasyMock API 文档,网址为 http://easymock.org/api/easymock /3.0/index.html。
Since you are mocking an interface, the only purpose in mocking a method would be to return a result from that method. In this case, it appears your 'send' method's return type is void. The 'EasyMock.expect' method is generic and expects a return type, which is causing the compiler to tell you that you can't use a void method because it doesn't have a return type.
For more information, see the EasyMock API documentation at http://easymock.org/api/easymock/3.0/index.html.
您不能以这种方式编写带有 void return 的方法脚本;查看这个问题,获取有关如何模拟
send< 行为的良好答案您的
预期
对象上的 /code> 方法。You can't script methods with a void return in that way; check out this question for a good answer on how you can mock the behavior of your
send
method on yourexpected
object.