简体   繁体   中英

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. 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.

@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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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