jeudi 20 août 2015

java testing servlet outside servlet container

I have the next problem. I want to test servlet (it works if I try manually via 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);
        }}}

My test:

 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());
        }
    }

Problem is:

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

I think the problem is that Tomcat calls init() method (and Spring initialise CategoriesDAO). In my case init() is not called and Autowired object is not initiated. My question is how to call init() outside Servlet Container?

Aucun commentaire:

Enregistrer un commentaire