Spring:控制器的测试处理程序方法:抛出特定的异常,预期嵌套的servlet异常?

时间:2016-03-28 22:07:48

标签: java spring junit

在我的控制器测试类中,当我强制抛出一个特定定义的异常时,我试图检查http响应中各种参数的值。 但是,我收到以下错误:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.my.exception.myServiceException
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:980)
...
Caused by: com.my.exception.myServiceException

myControllerTest.java:

@Test
public void testGetMethodException() throws Exception {
    when(myService.getMethod())
    .thenThrow(myServiceException.class);

     mockMvc.perform(get("/v1/method"))
           .andExpect(MockMvcResultMatchers.jsonPath("$._statusCode")
           .value(HttpStatus.BAD_REQUEST.toString()));

 }

1 个答案:

答案 0 :(得分:0)

这已经发生在我身上,查看堆栈跟踪,一个模拟对象正在引发NullPointerException,我建议查看堆栈跟踪的结尾。

例如:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException

at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:877)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:71)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
...
Caused by: java.lang.NullPointerException
    at java.time.LocalDate.now(LocalDate.java:225)
    at cl.forkchile.api.service.ScheduleService.canBeOpen(ScheduleService.java:24)
...