Joystick mock testing

Hey there,
Does anyone know why the following code gives back a nullPointerException when tested. To by the npe is the point of the when.thenReturn function.

A link to my github in case you would like to further see:

Thanks!

1 Like

Is mockedJoystick initialized?
Also what syntax is that? Doesn’t look like normal java.

Hey there!

Yes. MockJoystick was initialized and this is using the provided mock.ito methods. Ya, I agree that the syntax looks kinda weird, but this worked in my controlpanel test to over rule the color sensor.

Wow, mockito. That explains it. Sorry I have no clue but cool to see!

Hey Sumifairy,

When you mock something with mockito it initializes all the fields to null/0/false and doesn’t call the constructor. In the case of the joystick, it has an m_axis field that is null, and the getX() method is getting an NPE on that:

  public final double getX(Hand hand) {
    return getRawAxis(m_axes[Axis.kX.value]);
  }

This getX() method on the Joystick class is also final, so mockito can’t override it.

If you are able to use the sim HAL stuff, you might try using spy() on a real joystick object, instead of a mock. i.e.

mockedJoystick = spy(new Joystick(1));

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.