如何使用Spring测试框架测试非Spring控制器

时间:2015-01-08 20:44:46

标签: java spring java-ee spring-test spring-test-mvc

有人可以提供一些关于如何做的指导或代码示例吗?

HomePageController.java

public class HomePageController extends HttpServlet {

    private static final Logger log = Logger.getLogger(HomePageController.class);

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        log.debug("Into the HomePageController...");
        showPage(request, response, HOME_PAGE_URL);
    }

    private void showPage(HttpServletRequest request, HttpServletResponse response, String viewName) throws ServletException, IOException {
        log.debug("Displaying " + viewName + " page now...");
        String url = TEMPLATE_PAGE_URL + "?gotoPage=" + viewName;
        //forward the request to the page
        request.getServletContext().getRequestDispatcher(url).forward(request, response);
    }

}

0 个答案:

没有答案