Create a JsonProcessingException
Asked Answered
M

7

37

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?

Mindoro answered 27/5, 2015 at 6:18 Comment(2)
why don't you create a new class namely MyJsonProcessingException and inherit JsonProcessingException class?Daugherty
@Daugherty Why create a class when you don't have to?Hebraist
T
68

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.

Tamekia answered 13/11, 2017 at 23:5 Comment(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 SUTClarence
im a bit confused, what is the backets doing?Alliterate
@Alliterate It creates an anonymous class which extends JsonProcessingException. #28073547Orthopsychiatry
While it might do the same thing behind the scenes, the doThrow/when syntax suggested @albin-p-babu is cleaner.Hebraist
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?Stablish
D
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
Dinny answered 27/5, 2015 at 18:11 Comment(1)
this does not work at all, nor should it. JsonProcessingException is a checked exception, meaning that all subclasses of JsonProcessingException are checked exceptions as wellResinous
C
17

This one worked for me which allowed to throw JsonProcessingException itself

doThrow(JsonProcessingException.class).when(mockedObjectMapper).writeValueAsString(Mockito.any());
Chrissie answered 25/2, 2019 at 19:25 Comment(0)
K
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));
Kempe answered 3/1, 2020 at 23:56 Comment(0)
H
4

Faced the same issue today. You can use:

Mockito.when(mockObjectMapper.writeValueAsString(Mockito.any())).thenThrow(JsonProcessingException.class);
Hoffer answered 23/2, 2020 at 15:36 Comment(0)
K
-1

Try to use thenAnswer and create an anonymous class from JsonProcessingException

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

Kierstenkieselguhr answered 18/7, 2019 at 3:41 Comment(0)
M
-1

JsonUtils.toJson(new Object());

Marko answered 16/5, 2024 at 14:14 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.