I am trying to test my controller using mockMvc and mockito. The actual controller body is as following :
Message createXYZ(@RequestBody XYZ inst){
//creates XYZ
}
Now for pushing the values via request body, I am creating a JSON using GsonBuilder by serializing XYZ. Here is the structure for class XYZ :
class XYZ{
List<Y> listofYs;
//some other properties as well
}
I am creating and setting a mock for List listofYs and when Gson is trying to serialize an instance of XYZ that has a mocked listofYs its generating NPE.
Is there a way around or is it that I am doing it completely wrong?
According to the Mockito documentation, you can make a mock serializable:
List serializableMock = mock(List.class, withSettings().serializable());
Similarly to Stefan's answer above, mocks created via annotations can be marked as serializable through:
@Mock(serializable = true)
List serializableMock;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With