如何使用HttpUnit Servlet运行程序运行Servlet测试?在启动ServletUnit时遇到问题? [英] How to run the servlet test by using HttpUnit servlet runner? Problems in Starting up with ServletUnit?

查看:135
本文介绍了如何使用HttpUnit Servlet运行程序运行Servlet测试?在启动ServletUnit时遇到问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算通过ServletUnit对我的Servlet进行单元测试,并遇到了一些问题:
-作为起点,我们应该创建一个ServletRunner对象.构造函数之一期望File对象带有web.xml文件.我提供了我的web.xml文件的完整路径,但是以某种方式它忽略了所提供的路径并在顶级文件夹中搜索.代码段和错误消息如下:

I was planning to unit test my Servlet through ServletUnit and ran across some problems :
- As a starting point, we are supposed to create a ServletRunner object. One of the constructors expects File object with web.xml file. I provide the full path of my web.xml file but somehow it ignores the path provided and searches at the top level folder. The code-snippet and error message is below:

代码段

    ServletRunner sr = new ServletRunner(new File("* C:/eclipse-workspaces/pocs/lms-csd/src/main/webapp/WEB-INF/web.xml*")); 
ServletUnitClient sc = sr.newClient(); 
 WebRequest request = new PostMethodWebRequest("path to be specified" ); request.setParameter( "userId", "test" );
 request.setParameter( "password", "csd" );
  WebResponse response = sc.getResponse(request);
  String text = response.getText();

Assert.assertTrue(text.contains(欢迎离开管理系统"));

Assert.assertTrue(text.contains("Welcome to Leave Management System"));

堆栈跟踪

    com.meterware.httpunit.HttpInternalErrorException:
 Error on HTTP request: 500 org.apache.jasper.JasperException: java.io.FileNotFoundException: * C:\eclipse-workspaces\pocs\lms-csd\WEB-INF\web.xml* 
(The system cannot find the path specified)

[http://localhost/login]-为什么系统继续查看文件夹结构为.../WEB-INF/web.xml. 我的是一个Maven项目,我不想更改项目的结构以适应这种方式.如何使ServletRunner类从指定的文件夹中读取? -在Servlet代码中, 我使用以下代码:

[http://localhost/login] - Why does the system keep on looking at the folder structure to be .../WEB-INF/web.xml. Mine is a maven project and I would not like to change the structure of the project to adapt this way. How can I make ServletRunner class to read from a specified folder ? - In the Servlet code, I use the following code :

 String result = null if (someCondition) result = "/welcome.jsp"; } else { logger.warn("Password Validation failed"); request.setAttribute("failedlogin", new Boolean(true)); result = "/index.jsp"; } } RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher(result); requestDispatcher.forward(request, response); 

再次,尽管jsp文件存在于.../src/main/webapp/文件夹中,但ServletUnit仍希望welcome.jsp位于根目录.再次如何告知ServletUnit目标文件夹位置?

Again ServletUnit expects welcome.jsp to be at root foler, though jsp files are present at .../src/main/webapp/ folder. Again how can ServletUnit be told about the target folder location ?

非常感谢.

最好的问候 苏里·奈杜(M.SuriNaidu)

Best Regards M.SuriNaidu

推荐答案

这是我要做的事情.这是我的servlet测试的基类的传真.在这种情况下,我传递了源代码树中存在的web.xml文件的相对路径.我从ant和eclipse运行这些测试.

This is the sort of thing I do. This is a facsimile of the base class of my servlet tests. In this case I pass the relative path of the web.xml file as it exists in my source tree. I run these tests from ant and eclipse.

abstract public class ServletTestCase {

    protected ServletRunner       m_runner;
    protected ServletUnitClient   m_client;
    protected String              m_userAgent = "something/1.0";

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        initHttpUnit();
    }

    @Override
    protected void tearDown() throws Exception {
        shutdownHttpUnit();
        super.tearDown();
    }

    protected void initHttpUnit() throws IOException, SAXException {
        shutdownHttpUnit();

        // We are expecting UTF-8 character handling in URLs, make it the default
        HttpUnitOptions.setDefaultCharacterSet("UTF-8");

        // Find the servlet's web.xml file and use it to init servletunit
        File file = new File("tests/web.xml"));
        m_runner = new ServletRunner(file);
        m_client = m_runner.newClient();
        m_client.getClientProperties().setUserAgent(m_userAgent);
    }

    protected void shutdownHttpUnit() {
        if (m_runner != null) {
            m_runner.shutDown();
        }
        m_client = null;
        m_runner = null;
    }
}

这篇关于如何使用HttpUnit Servlet运行程序运行Servlet测试?在启动ServletUnit时遇到问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆