java测试servlet容器之外的servlet

时间:2015-08-20 15:43:25

标签: java spring unit-testing servlets

我有下一个问题。我想测试servlet(如果我通过Tomcat手动尝试,它可以工作):

package controller;

import blabla

@Controller
public class MainServlet extends HttpServlet {

    @Autowired
    private CategoriesDAO categoriesDAO;


    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println("Called init");//In my case it is not called
        super.init();
            SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = getAction(req);
        HttpSession session = req.getSession(false);

        if (action.startsWith("xxx")) {
            List<Category> categories = categories.getCategories();
            req.setAttribute("categories", categories);

            req.getRequestDispatcher("xxx.jsp").forward(req, resp);
        }}}

我的测试:

 package servlet;

    import blabla

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath:application-context-test.xml"})
    public class MainServletTest extends TestCase{

        @Autowired
        private CategoriesDAO categories;

        @Mock
        private HttpServletRequest request;

        @Before
        public void setUp() throws Exception {
           MockitoAnnotations.initMocks(this);
           categories.truncate();
           categories.setLoaded(false);
           Project.projectid = 0;
           Category.categoryid = 0;
        }

        @Mock
        private HttpServletResponse response;

        @Mock
        private HttpSession session;

        @Mock
        private RequestDispatcher rd;

        @Test
        public void testCategories() throws ServletException, IOException {
            // given
            categoriesDAO.add(new Category("name3"));
            categoriesDAO.add(new Category("name4"));
            when(request.getRequestURI()).thenReturn("/test/xxx");
            when(request.getContextPath()).thenReturn("/test");
            when(request.getSession()).thenReturn(session);
            //when

            MainServlet main = new MainServlet();
            main.init(getServletConfig());//This line doesnot work. My tries
            main.doGet(request, response);

            //then
            verify(request).setAttribute("categories", categories.getCategories());
        }
    }

问题是:

java.lang.NullPointerException
    at controller.MainServlet.doGet(MainServlet.java:48)
    at servlet.MainServletTest.testCategories(MainServletTest.java:67)
    blabla

我认为问题在于Tomcat调用init()方法(和Spring初始化CategoriesDAO)。在我的情况下, init()未被调用,并且未启动Autowired对象。我的问题是如何在Servlet容器外调用 init()

2 个答案:

答案 0 :(得分:0)

我认为您还需要将已定义DAO的xml文件添加到@ContextConfiguration。好像

 @ContextConfiguration(locations = {"classpath:application-context-test.xml","classpath*:dao.xml"})

答案 1 :(得分:0)

我发现有一个名为Cactus的JUnit Extension。它可用于测试没有容器的servlet: http://www.tutorialspoint.com/junit/junit_extensions.htm