I'm trying to use this mock in a test class:
@Mock
private IDPool idPool;
@Before
private void before() {
expect(idPool.getNextFree()).andReturn(TRACK_ID).anyTimes(); //line 63
}
When I run it I get NPE:
java.lang.NullPointerException
at com.mycompany.IDPool.getNextFree(IDPool.java:68)
at com.mycompany.Test.before(Test.java:63)
It looks like it tries to run the production code instead of mock? Because the NPE is thrown here in IDPool class:
public final Integer getNextFree() {
int nextFree;
synchronized (this.usedIDs) { //line 68
//...
}
}
I thought mocks don't use actual implementations? can I mock this object so that it doesn't?
I would comment, but I don't have enough reputation.
Your suspicion about EasyMock not being able to mock the final method (or class) is right. However, you can use PowerMock. I cannot verify it now, but initializing the mock with
PowerMock.createMock(IDPool.class)should enable final methods mocking.This answer and documentation should help you.