vendredi 6 avril 2018

Rest api exception handler test

I am stuck with how to test that an exception handler works, when i use curl i see that the exception work but i want to write a test for it , since its a rest controller i tried mock mvc test but i get a nested servlet exception bellow contains details here is my exception handler from controller advice class

    @ExceptionHandler(DataAccessException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public @ResponseBody String handleDataAccessException(
                                                    DataAccessException e) {
        LOGGER.debug("handleDataAccessException({})", e);
        return "DataAccessException: " + e.getLocalizedMessage();
    }

and here is my controller

 @GetMapping("/orders/{orderId}")
    @ResponseStatus(HttpStatus.FOUND)
    public final OrderDto getOrderId(@PathVariable(value = "orderId")
                                                    final Integer orderId) {
        LOGGER.debug("getOrderId({})", orderId);

        // get order then map to dto
        Order order = orderService.getOrderById(orderId);

        return mappingService.map(order, OrderDto.class);
    }

This is the test i wrote that doesnt work

@Rule
    public final ExpectedException exception = ExpectedException.none();

@Test
     public void getOrderByIdException() throws Exception {
         LOGGER.debug("test: getOrderById() ");

         when(orderService.getOrderById(1)).thenThrow(new DataAccessException("") {
         });

         mockMvc.perform(get("/orders/{orderId}", 1))
        .andExpect(status().isNotFound());

         exception.expect(DataAccessException.class);
         Mockito.verify(orderService).getOrderById(1);
     }

stack trace

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.epam.brest.course.rest.OrderRestControllerMockTest$1: 

    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:65)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:160)
    at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:127)
    at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:155)
    at com.epam.brest.course.rest.OrderRestControllerMockTest.getOrderByIdException(OrderRestControllerMockTest.java:227)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.sp

Aucun commentaire:

Enregistrer un commentaire