Skip to content
Advertisement

Mockito:: Unit test case for JsonProcessingException when passing object

I have a class as shown below:

JavaScript

Now I am trying to write a unit test case for this where I can throw exception and unit test the catch clause using Mockito but I am not able to mock it. I have noticed that object mapper class internally converting everything to string even if I set name and age to null. Can anyone please help me with this?

Advertisement

Answer

Use constructor injection with Spring. This will allow you to inject mocked ObjectMapper in the class, which then you can set behavior in the test. See below.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement