我查看了发布的答案,并认为我会发布一个更完整的解决方案,以实际演示如何使用嵌入式 GlassFish 及其 Apache Maven 插件进行测试。
我在我的博客Using GlassFish 3.1.1 Embedded with JUnit 4.x and HtmlUnit 2.x 上写了完整的过程,并将完整的项目放在Bitbucket 上供下载:image-servlet
在我看到这个问题之前,我正在查看关于 JSP/JSF 标记的图像 servlet 的另一篇文章。所以我将我在另一篇文章中使用的解决方案与这篇文章的完整单元测试版本结合起来。
如何测试
Apache Maven 具有明确定义的生命周期,其中包括 test。我将使用它和另一个名为 integration-test 的生命周期来实现我的解决方案。
- 在 surefire 插件中禁用标准生命周期单元测试。
- 添加
integration-test作为surefire插件执行的一部分
- 将 GlassFish Maven 插件添加到 POM。
- 将 GlassFish 配置为在
integration-test 生命周期内执行。
- 运行单元测试(集成测试)。
GlassFish 插件
将此插件添加为<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>
Surefire 插件
添加/修改插件作为<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>
HTML 单元
添加集成测试,如下例所示。
@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();
}
我将完整的过程写在我的博客Using GlassFish 3.1.1 Embedded with JUnit 4.x and HtmlUnit 2.x 上,并将完整的项目放在Bitbucket 上供下载:image-servlet
如果您有任何问题,请发表评论。我认为这是一个完整的示例,您可以将其用作您计划对 servlet 进行的任何测试的基础。