在非常简单的示例中使用 EasyMock.expect() 时出现编译错误?

发布于 2024-12-01 14:04:54 字数 566 浏览 6 评论 0原文

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

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

发布评论

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

评论(4

趁年轻赶紧闹 2024-12-08 14:04:54

如果您想测试 void 方法,请调用您想要在模拟上测试的方法。然后调用 expectLastCall()方法。

这是一个例子:

@Test
public void testSomething()
{
    SomeInterface mock = EasyMock.createMock(SomeInterface.class);
    SomeBase expected = new DerivesFromSomeBase();

    mock.send(expected);

    EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
        public Object answer() {
            // do additional assertions here
            SomeBase arg1 = (SomeBase) EasyMock.getCurrentArguments()[0];

            // return null because of void
            return null;
        }
    });
}

If you want to test void methods, call the method you want to test on your mock. Then call the expectLastCall() method.

Here's an example:

@Test
public void testSomething()
{
    SomeInterface mock = EasyMock.createMock(SomeInterface.class);
    SomeBase expected = new DerivesFromSomeBase();

    mock.send(expected);

    EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
        public Object answer() {
            // do additional assertions here
            SomeBase arg1 = (SomeBase) EasyMock.getCurrentArguments()[0];

            // return null because of void
            return null;
        }
    });
}
假装不在乎 2024-12-08 14:04:54

由于您的 send() 方法返回 void,因此只需使用预期值调用模拟方法并重播即可:

SomeInterface mock = EasyMock.createMock(SomeInterface.class);
SomeBase expected = new DerivesFromSomeBase(); 
mock.send(expected);
replay(mock);

Since your send() method returns void, just call the mock method with expected values and replay:

SomeInterface mock = EasyMock.createMock(SomeInterface.class);
SomeBase expected = new DerivesFromSomeBase(); 
mock.send(expected);
replay(mock);
走走停停 2024-12-08 14:04:54

由于您正在模拟接口,因此模拟方法的唯一目的是从该方法返回结果。在这种情况下,您的“发送”方法的返回类型似乎为 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.

神也荒唐 2024-12-08 14:04:54

您不能以这种方式编写带有 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 your expected object.

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