giz*_*zmo 53 java unit-testing servlets
我想知道对servlet进行单元测试的最佳方法是什么.
测试内部方法不是问题,只要它们不引用servlet上下文,而是测试doGet/doPost方法以及引用上下文或使用会话参数的内部方法呢?
有没有办法简单地使用经典工具,如JUnit,或者最好是TestNG?我是否需要嵌入tomcat服务器或类似的东西?
Gar*_*our 45
大多数时候我通过"集成测试"而不是纯粹的单元测试来测试Servlet和JSP.JUnit/TestNG有大量附加功能,包括:
这是一个简单的Order Processing Servlet的JWebUnit测试,它处理来自'orderEntry.html'形式的输入.它需要客户ID,客户名称和一个或多个订单商品:
public class OrdersPageTest {
    private static final String WEBSITE_URL = "http://localhost:8080/demo1";
    @Before
    public void start() {
        webTester = new WebTester();
        webTester.setTestingEngineKey(TestingEngineRegistry.TESTING_ENGINE_HTMLUNIT);
        webTester.getTestContext().setBaseUrl(WEBSITE_URL);
    }
    @Test
    public void sanity() throws Exception {
        webTester.beginAt("/orderEntry.html");
        webTester.assertTitleEquals("Order Entry Form");
    }
    @Test
    public void idIsRequired() throws Exception {
        webTester.beginAt("/orderEntry.html");
        webTester.submit();
        webTester.assertTextPresent("ID Missing!");
    }
    @Test
    public void nameIsRequired() throws Exception {
        webTester.beginAt("/orderEntry.html");
        webTester.setTextField("id","AB12");
        webTester.submit();
        webTester.assertTextPresent("Name Missing!");
    }
    @Test
    public void validOrderSucceeds() throws Exception {
        webTester.beginAt("/orderEntry.html");
        webTester.setTextField("id","AB12");
        webTester.setTextField("name","Joe Bloggs");
        //fill in order line one
        webTester.setTextField("lineOneItemNumber", "AA");
        webTester.setTextField("lineOneQuantity", "12");
        webTester.setTextField("lineOneUnitPrice", "3.4");
        //fill in order line two
        webTester.setTextField("lineTwoItemNumber", "BB");
        webTester.setTextField("lineTwoQuantity", "14");
        webTester.setTextField("lineTwoUnitPrice", "5.6");
        webTester.submit();
        webTester.assertTextPresent("Total: 119.20");
    }
    private WebTester webTester;
}
Joh*_*ary 10
我查看了发布的答案,并认为我会发布一个更完整的解决方案,实际演示了如何使用嵌入式GlassFish及其Apache Maven插件进行测试.
我在我的博客上编写了完整的过程使用GlassFish 3.1.1嵌入了JUnit 4.x和HtmlUnit 2.x,并将完整的项目下载到Bitbucket:image-servlet
在我看到这个问题之前,我正在查看JSP/JSF标签的图像servlet上的另一篇文章.因此,我将其他帖子中使用的解决方案与此帖子的完整单元测试版本相结合.
Apache Maven具有明确定义的生命周期,包括test.我将使用它和另一个生命周期integration-test来实现我的解决方案.
integration-test为surefire-plugin执行的一部分integration-test生命周期中执行.添加此插件作为一部分<build>.
        <plugin>
            <groupId>org.glassfish</groupId>
            <artifactId>maven-embedded-glassfish-plugin</artifactId>
            <version>3.1.1</version>
            <configuration>
                <!-- This sets the path to use the war file we have built in the target directory -->
                <app>target/${project.build.finalName}</app>
                <port>8080</port>
                <!-- This sets the context root, e.g. http://localhost:8080/test/ -->
                <contextRoot>test</contextRoot>
                <!-- This deletes the temporary files during GlassFish shutdown. -->
                <autoDelete>true</autoDelete>
            </configuration>
            <executions>
                <execution>
                    <id>start</id>
                    <!-- We implement the integration testing by setting up our GlassFish instance to start and deploy our application. -->
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>start</goal>
                        <goal>deploy</goal>
                    </goals>
                </execution>
                <execution>
                    <id>stop</id>
                    <!-- After integration testing we undeploy the application and shutdown GlassFish gracefully. -->
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>undeploy</goal>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
添加/修改插件作为一部分<build>.
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12.4</version>
            <!-- We are skipping the default test lifecycle and will test later during integration-test -->
            <configuration>
                <skip>true</skip>
            </configuration>
            <executions>
                <execution>
                    <phase>integration-test</phase>
                    <goals>
                        <!-- During the integration test we will execute surefire:test -->
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <!-- This enables the tests which were disabled previously. -->
                        <skip>false</skip>
                    </configuration>
                </execution>
            </executions>
        </plugin>
添加集成测试,如下例所示.
@Test
public void badRequest() throws IOException {
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    webClient.getOptions().setPrintContentOnFailingStatusCode(false);
    final HtmlPage page = webClient.getPage("http://localhost:8080/test/images/");
    final WebResponse response = page.getWebResponse();
    assertEquals(400, response.getStatusCode());
    assertEquals("An image name is required.", response.getStatusMessage());
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(true);
    webClient.getOptions().setPrintContentOnFailingStatusCode(true);
    webClient.closeAllWindows();
}
我在我的博客上编写了完整的过程使用GlassFish 3.1.1嵌入了JUnit 4.x和HtmlUnit 2.x,并将完整的项目下载到Bitbucket:image-servlet
如果您有任何疑问,请发表评论.我认为这是一个完整的示例,您可以将其用作规划servlet的任何测试的基础.
你是在单元测试中手动调用doPost和doGet方法吗?如果是这样,您可以覆盖HttpServletRequest方法以提供模拟对象.
myServlet.doGet(new HttpServletRequestWrapper() {
     public HttpSession getSession() {
         return mockSession;
     }
     ...
}
该了HttpServletRequestWrapper是一个方便的Java类.我建议你在单元测试中创建一个实用工具方法来创建模拟http请求:
public void testSomething() {
    myServlet.doGet(createMockRequest(), createMockResponse());
}
protected HttpServletRequest createMockRequest() {
   HttpServletRequest request = new HttpServletRequestWrapper() {
        //overrided methods   
   }
}
将模拟创建方法放在基本servlet超类中并使所有servlet单元测试扩展它更好.
Mockrunner(http://mockrunner.sourceforge.net/index.html)可以做到这一点.它提供了一个可用于测试Servlet的模拟J2EE容器.它还可以用于单元测试其他服务器端代码,如EJB,JDBC,JMS,Struts.我自己只使用了JDBC和EJB功能.
| 归档时间: | 
 | 
| 查看次数: | 47779 次 | 
| 最近记录: |