执行模拟控制器时发生NullPointerexception

时间:2018-11-22 10:27:23

标签: mockito

我的控制器类中包含以下代码

public class GoodsController{

@AutoWired
Private GoodsService goodsService;
public ResponseEntity<String> startCall(HttpServletRequest request) {

        ResponseEntity<String> responseEntity = null;

        try {
            ResponseBean respBean = goodsService.startCall(request);
            HttpHeaders responseHeaders = new HttpHeaders();

            if(request.getAttribute(SessionConstants.GOODS_SESS) != null){
                responseHeaders.set(SessionConstants.GOODS_SESS,      request.getAttribute(SessionConstants.GOODS_SESS).toString());
            }

            responseEntity = new ResponseEntity<>(SessionUtil.objToJson(respBean, respBean), responseHeaders,
                    HttpStatus.valueOf(respBean.getStatus()));

            return responseEntity;

        } catch (AlightSessionException e) {
            StringBuilder message = new StringBuilder("{\"message\": \"error:\""+e.getMessage()+", ")
                    .append("\"status\":\"400\"}");
            responseEntity = new ResponseEntity<>(message.toString(), HttpStatus.BAD_REQUEST);
            ErrorLogEventHelper.logErrorEvent(SessionController.class.getName(), message.toString(), "startSessionForPerson()", e,
                    ErrorLogEvent.ERROR_SEVERITY);      
            return responseEntity;
        }

    }
}

我的TestClass中有以下代码

   public class GoodsControllerTest{

    MockHttpServletRequestBuilder request = 

    MockMvcRequestBuilders
                    .post("/startCall").sessionAttr("personToken", "eyjpc");

            ResultActions result = mockMvc.perform(request);
             result.andExpect(MockMvcResultMatchers.status().isOk());

    }
  }

我在执行过程中遇到以下错误,请帮帮我。

  

org.springframework.web.util.NestedServletException:请求处理失败;嵌套的异常是java.lang.NullPointerException         在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)         在org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)         在javax.servlet.http.HttpServlet.service(HttpServlet.java:648)         在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)         在org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:65)         在javax.servlet.http.HttpServlet.service(HttpServlet.java:729)         在org.springframework.mock.web.MockFilterChain $ ServletFilterProxy.doFilter(MockFilterChain.java:167)         在org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134)         在org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:155)         在com.alight.upoint.sessionservice.controller.SessionControllerTest.testGetEligiblePlans(SessionControllerTest.java:57)         在org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)         在org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)         在org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)         在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)         在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)         在org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)         在org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)         在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)       造成原因:java.lang.NullPointerException         在com.alight.upoint.sessionservice.controller.SessionController.startSessionForPerson(SessionController.java:51)         在org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)         在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)         在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)         在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)         在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)         在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)         在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)         在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)         在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)         ...还有17个

1 个答案:

答案 0 :(得分:0)

您需要使用MockMvcBuilders.standaloneSetup与您的控制器一起创建应用程序上下文,然后使用此mockMcv调用您的控制器API,即方法

您可以参考this post了解更多详细信息。