简体   繁体   English

EasyMock 在最终方法上抛出不兼容的返回值类型

[英]EasyMock throwing incompatible return value type on final method

Basically, I'm trying to set the result of a final method.基本上,我正在尝试设置最终方法的结果。 The method is simple, like this:方法很简单,像这样:

@NotNull
public final Server getServer() {
    return this.server;
}

I mock it like this:我这样嘲笑它:

EasyMock.expect(object.getServer()).andReturn(server);

Where server is a POJO.服务器是一个POJO。 This throws this:这抛出这个:

java.lang.IllegalStateException: incompatible return value type
    at org.easymock.internal.MocksControl.andReturn(MocksControl.java:281)

For that line.对于那条线。 I tried not using a POJO for server, and mocking it instead.我尝试不将 POJO 用于服务器,而是模拟它。

@Mock
private Server server = mock(Server.class);

Yet still the same error.然而还是同样的错误。 I'm absolutely positive that they are the exact same type.我绝对肯定它们是完全相同的类型。 Why is this happening?为什么会这样?

Now, for some reason I get a different error:现在,出于某种原因,我得到了一个不同的错误:

java.lang.IllegalStateException: no last call on a mock available

Same code, all I have is this:相同的代码,我只有这个:

@Test
public void test() {
    EasyMock.expect(object.getServer()).andReturn(server);
    replayAll();
    TestedObject.useObject(object);
}

The reason you get an error when mocking a final method is that it is not supported by EasyMock.在模拟 final 方法时出现错误的原因是 EasyMock 不支持它。 https://easymock.org/user-guide.html#mocking-limitations https://easymock.org/user-guide.html#mocking-limitations

Final methods cannot be mocked.最终方法不能被模拟。 If called, their normal code will be executed.如果调用,它们的正常代码将被执行。

You can use PowerMock to mock final and static methods.您可以使用 PowerMock 来模拟最终和静态方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM