how about throwing an anonymous exception like JsonProcessingException
when(mapper.writeValueAsString(any(Object.class))).thenThrow(new JsonProcessingException("Error"){});
The braces {} do the trick. This is much better since it does not confuse the reader of the test code.
source share