-1

I have a Custom exception that handles the expected errors of my program and here is the code.

@ControllerAdvice
@RestController
public class DashboardException {

@ExceptionHandler({Exception.class, IOException.class, ParseException.class, JsonProcessingException.class})
public final ResponseEntity<ErrorDetails> dataNotFoundException(Exception ex, WebRequest request) {
    ErrorDetails errorDetails = new ErrorDetails();
    errorDetails.setTimestamp(new Date().toString());
    errorDetails.setMessage(ex.getMessage());
    errorDetails.setPath(request.getDescription(false));
    errorDetails.setStatus(HttpStatus.BAD_REQUEST.value());
    errorDetails.setError(HttpStatus.BAD_REQUEST);
    return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST);
}
   }

My problem is on how to properly unit test this class. This is what I have made so far to make it cover, but with no luck.

 @RunWith(SpringJUnit4ClassRunner.class)

@WebAppConfiguration

@ContextConfiguration(classes = { DashboardException.class, TestConfiguration.class, DataController.class })
public class testDashboardException {

    private MockMvc mockMvc;

    @Autowired
    WebApplicationContext wac;

    @Before
    public void setUp() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    @Configuration
      @EnableWebMvc
      public static class TestConfiguration { }

     @Controller
      @RequestMapping("/tests")
      public static class RestProcessingExceptionThrowingController {


        @GetMapping(value = "/exception")
        public @ResponseBody String find() throws Exception {
          throw new Exception("global_error_test");
        }
      }


      @Test
      public void testHandleException() throws Exception {
        mockMvc.perform(get("/tests/exception"))
            .andExpect(new ResultMatcher() {


              @Override
              public void match(MvcResult result) throws Exception {
                result.getResponse().getContentAsString().contains("global_error_test");
              }
            })
            .andExpect(status().isBadRequest());
      }

    /*
     * @Test public void testErrorDetailsValue() {
     * 
     * thrown.expect(Exception.class); thrown.expect(IOException.class);
     * thrown.expect(ParseException.class);
     * thrown.expect(JsonProcessingException.class);
     * 
     * thrown.expectMessage("Bad Request");
     * 
     * }
     */
}

I only have a little knowledge concerning custom exceptions. What am I missing here? Thanks for any assistance.

2
  • You may want to take a look at stackoverflow.com/questions/16669356/…
    – dzhg
    Jan 8, 2020 at 7:05
  • What you are missing here is a clear and detailed description of what you don't understand. "It doesn't work" is never an appropriate error description. Jan 9, 2020 at 8:27

1 Answer 1

0

I found out how to cover my custom exception. I just included a test on my controller that will fail the endpoint and it did catch an exception and covered my custom exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.