-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.

Ronald
  • 37
  • 1
  • 7
  • You may want to take a look at https://stackoverflow.com/questions/16669356/testing-spring-mvc-exceptionhandler-method-with-spring-mvc-test/33311231 – dzhg Jan 08 '20 at 07: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. – Roland Illig Jan 09 '20 at 08:27

1 Answers1

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.

Ronald
  • 37
  • 1
  • 7