0

I have many hours struggling with this problem.

I have the following line of code:

Mockito.doReturn(new ErrorResponse()).when(mockobjectMapper).readValue("Test", ErrorResponse.class);

The method "readValue" spits an error:

Unhandled exceptions: com.fasterxml.jackson.core.JsonProcessingException, com.fasterxml.jackson.databind.JsonMappingException

I can surround it with try-catch block, but I have many different classes which will have the same catch. For example:

try {
        Mockito.doReturn(someObject1).when(someMock).execute(Mockito.eq(someObject2));
     } catch (SomeInterfaceException e) {
        // Will never be reached
        log.error("{}", StructuredArguments.fields(e));
     }

How can I move the try-catch block to an utils class with a method which will receive the line of code which spits the error?

Evoldev
  • 35
  • 5
  • The question is why you would want to do this. A least specifically for JUnit testing, it will catch the exceptions for you (and log them as errors). – markspace Dec 28 '21 at 00:17
  • @markspace I am doing JMH tests, so I have this try-catch through multiple performance tests, with the same catch message... I would like to do it in order to avoid repeated code and just call the abstraction by sending the line (which is spitting the error) by parameter. – Evoldev Dec 28 '21 at 00:19

0 Answers0