在我的控制器测试类中,当我强制抛出一个特定定义的异常时,我试图检查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()));
}
答案 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)
...