I found a similar question here: Mockito when()...then() NullPointerException but in that question method to be tested was static.
I have a class with a boolean method to be tested. The test class gives NULLPOINTEREXCEPTION on the when()..then() line.
Thanks for your help.
Class to be tested
public class FMBaseController implements FMHandler {
private PlayerHandler player;
public FMBaseController(PlayerHandler player) {
this.player = player;
}
@Override
public boolean boostValue(FamilyMember fm, int increase) {
if (player.getResourceHandler().getServants() < increase)
return false;
return true;
Tested class
public class FMBaseControllerTest {
//class to test
private FMBaseController fmBase;
//dependencies (these will be mocked)
private FamilyMember fm;
private PlayerHandler playerHandler;
@Before
public void setUp() throws Exception {
fm = mock(FamilyMember.class);
playerHandler = mock(PlayerHandler.class);
fmBase = new FMBaseController(playerHandler);
}
@Test
public void boostValueTest() {
when(playerHandler.getResourceHandler().getServants()).thenReturn(3).thenReturn(5);
//3 is less than 4 . assert you cannot boost
Boolean bool1 = fmBase.boostValue(fm, 4);
assertFalse( bool1 );
//5 is not less than 4 . assert you can boost
Boolean bool2 = fmBase.boostValue(fm, 4);
assertTrue( bool2 );
}
}
FAILURE TRACE
FMBaseControllerTest
******.server.controller.FMBaseControllerTest
boostValueTest(******.server.controller.FMBaseControllerTest)
java.lang.NullPointerException
at ******.server.controller.FMBaseControllerTest.boostValueTest(FMBaseControllerTest.java:44)