You can not mock final class based on this link : https://github.com/mockito/mockito/wiki/FAQ#what-are-the-limitations-of-mockito
see these links:
How to mock a final class with mockito
How to mock a final class with mockito
try to use Power Mockito as follow:
public final class Plane {
public static final int ENGINE_ID_RIGHT = 2;
public static final int ENGINE_ID_LEFT = 1;
public boolean verifyAllSystems() {
throw new UnsupportedOperationException("Fail if not mocked!");
}
public void startEngine(int engineId) {
throw new UnsupportedOperationException(
"Fail if not mocked! [engineId=" + engineId + "]");
}
}
public class Pilot {
private Plane plane;
public Pilot(Plane plane) {
this.plane = plane;
}
public boolean readyForFlight() {
plane.startEngine(Plane.ENGINE_ID_LEFT);
plane.startEngine(Plane.ENGINE_ID_RIGHT);
return plane.verifyAllSystems();
}
}
and test final class:
@PrepareForTest(Plane.class)
public class PilotTest extends PowerMockTestCase {
@Test
public void testReadyForFlight() {
Plane planeMock = PowerMockito.mock(Plane.class);
Pilot pilot = new Pilot(planeMock);
Mockito.when(planeMock.verifyAllSystems()).thenReturn(true);
// testing method
boolean actualStatus = pilot.readyForFlight();
Assert.assertEquals(actualStatus, true);
Mockito.verify(planeMock).startEngine(Plane.ENGINE_ID_LEFT);
Mockito.verify(planeMock).startEngine(Plane.ENGINE_ID_RIGHT);
}
}
example link : https://dzone.com/articles/mock-final-class