I have a class as shown below:
@Getter
@Setter
public class User{
@Autowired
Logger log;
private String name;
private String age;
public String toJson(){
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = null;
try{
jsonString = objectMapper.writeValueAsString(this);
}catch(JsonProcessingException jsnEx){
log.writeErr(" Error while parsing the Json" + jsnEx.getMessage());
}
return jsonString;
}
}
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?