36

I'm trying to create a JsonProcessingException to be thrown by a mock object.

when(mapper.writeValueAsString(any(Object.class))).thenThrow(new JsonProcessingException("Error"));

However I'm unable to create a JsonProcessingException object as all the constructors are protected. How do I get around this?

pez
  • 1,034
  • 2
  • 10
  • 23

6 Answers6

65

how about you create an anonymous exception of type JsonProcessingException

when(mapper.writeValueAsString(any(Object.class))).thenThrow(new JsonProcessingException("Error"){});

The {} braces does the trick. This is much better since it is not confusing to the reader of the test code.

rajan.jana
  • 674
  • 6
  • 3
  • 5
    this will do the trick (code will compile), but at the same time will result in `Checked exception is invalid for this method!`. Real solution will involve extending ObjectMapper in your test method and passing that to the SUT – senseiwu Dec 10 '18 at 15:09
  • 1
    im a bit confused, what is the backets doing? – Robbo_UK Jul 11 '19 at 09:50
  • 2
    @Robbo_UK It creates an anonymous class which extends `JsonProcessingException`. https://stackoverflow.com/questions/28073047/anonymous-initialization-of-class-with-protected-constructor – xli Apr 28 '20 at 23:17
  • 1
    While it might do the same thing behind the scenes, the `doThrow/when` syntax suggested @albin-p-babu is cleaner. – ithinkisam Oct 29 '20 at 19:10
  • I'm seeing the following error when I try this: `unreported exception com.fasterxml.jackson.core.JsonProcessingException; must be caught or declared to be thrown`. Any ideas how to fix this? – Beau Forest Dec 04 '22 at 04:04
20

How about throwing one of the known direct subclasses instead?

for v1.0

Direct Known Subclasses:
JsonGenerationException, JsonMappingException, JsonParseException

for v2.0

Direct Known Subclasses:
JsonGenerationException, JsonParseException
Jose Martinez
  • 11,452
  • 7
  • 53
  • 68
  • Works Perfectly! Thanks. – pez May 28 '15 at 00:03
  • this does not work at all, nor should it. JsonProcessingException is a checked exception, meaning that all subclasses of JsonProcessingException are checked exceptions as well – DanDan Aug 05 '19 at 10:11
14

This one worked for me which allowed to throw JsonProcessingException itself

doThrow(JsonProcessingException.class).when(mockedObjectMapper).writeValueAsString(Mockito.any());
PLASMA chicken
  • 2,777
  • 2
  • 15
  • 25
Albin
  • 277
  • 5
  • 19
8

I came across this issue today also. The simplest and cleanest solution I came up with was to create and throw a mock JsonProcessingException

when(mapper.writeValueAsString(any(Object.class)))
    .thenThrow(mock(JsonProcessingException.class));
spottedmahn
  • 14,823
  • 13
  • 108
  • 178
Aaron McGhie
  • 81
  • 1
  • 1
4

Faced the same issue today. You can use:

Mockito.when(mockObjectMapper.writeValueAsString(Mockito.any())).thenThrow(JsonProcessingException.class);
StoneCold
  • 41
  • 2
-1

Try to use thenAnswer and create an anonymous class from JsonProcessingException

when(mapper.writeValueAsString(any(Object.class))).thenAnswer(x-> {throw new JsonProcessingException(""){};});