简体   繁体   English

使用Mockito的NullPointer异常测试Servlet

[英]NullPointer exception testing servlet using mockito

I am using mockito to test a servlet 我正在使用Mockito测试Servlet

The doGet method of my servlet is 我的servlet的doGet方法是

@Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
                response.setContentType("application/json");
                PrintWriter out = response.getWriter();
                String servletPath = request.getServletPath();
                String servletInfo = request.getPathInfo();
                List<SchoolProperties> itemList = itemService.findAll();
                ItemParser itemParser = new ItemParser();
                itemParser.setJsonFactory(new JsonFactory());
                out.println(itemParser.parseItem(itemList));

        }

My test method is 我的测试方法是

   @Test
    public void shouldPrintJsonString throws IOException, ServletException{
        HttpServletRequest req = mock(HttpServletRequest.class);
        HttpServletResponse res = mock(HttpServletResponse.class);
        ItemService is = mock(ItemService.class);
        ArrayList mockList = mock(ArrayList.class);

        SchoolProperties s1 = new SchoolProperties(1, "MyItemName", 5);
        SchoolProperties s2 = new SchoolProperties(1, "MyItemName", 5);
        mockList.add(s1);
        mockList.add(s2);



        StringWriter writer = new StringWriter();
        when(req.getServletPath()).thenReturn("/school_items");
        when(req.getMethod()).thenReturn("GET");
        when(req.getPathInfo()).thenReturn("/");
        when(is.findAll()).thenReturn(mockList);
        given(res.getWriter()).willReturn(new PrintWriter(writer));

        ItemController it = new ItemController();
        it.setItemService(is);

        it.service(req, res);
        String expectedResult="";
        verify(res).setContentType("application/json");
        verify(is.findAll(),atLeast(1));
        Assert.assertEquals(expectedResult, writer.toString());
        Assert.assertTrue(writer.toString().contains("["));
}

I am getting a nullpointer exception when running my test case. 运行测试用例时,我得到了一个nullpointer异常。 I suspect that my ItemParser and JsonFactory are not created in the test or is not injected. 我怀疑我的ItemParser和JsonFactory未在测试中创建或未注入。 Is this correct? 这个对吗? If so how to make it so that it won't be null without changing the servlet method? 如果是这样,如何在不更改servlet方法的情况下使其不为null?

Your problem is that you've mocked the ArrayList , which means that its add method will do nothing, so s1 and s2 are never in the list. 您的问题是您嘲笑了ArrayList ,这意味着它的add方法将什么都不做,因此s1s2永远s2列表中。 Don't mock ArrayList - use a real ArrayList instead. 别拿ArrayList -使用真正ArrayList来代替。

Also verify(is.findAll(),atLeast(1)); 还要verify(is.findAll(),atLeast(1)); should read verify(is,atLeast(1)).findAll(); 应该读verify(is,atLeast(1)).findAll();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM