Introduction
I was looking for an example of how to use GlassFish Embedded Maven plugin to integrate with HtmlUnit(JUnit) for doing testing. I was surprised that I really didn't find anything that seemed very complete. There were a number of examples that showed how to use the Embedded EJB Container in tests, but nothing that really showed how to use the Maven plugin. While searching I came across a question on stackoverflow asking how about Unit testing a Java Servlet. I decided to kill two birds with one stone; (1) write up a good example for Glassfish, and (2) help someone else with a complete solutionThe testing here is implemented as part of the
integration-test
lifecycle of Maven. This does not replace standard JUnit testing of your code, but rather provides testing of your web application as you would expect in deployment. Consider this a valuable supplement to your testing strategy.The complete project is located here: image-servlet.
Requirements
- First make sure that you configure the Maven plugin. Complete details can be found in Oracle GlassFish Server Embedded Server Guide
1234567891011121314151617181920212223242526272829303132333435363738<
build
>
<
plugins
>
<
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
>
</
plugins
>
</
build
>
- Next, configure the surefire plugin to skip its normal test cycle.
12345678910111213141516171819202122<
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
>
- Add a dependencies for JUnit and HtmlUnit to the pom.xml
123456789101112<
dependency
>
<
groupid
>junit</
groupId
>
<
artifactid
>junit</
artifactId
>
<
version
>4.11</
version
>
<
scope
>test</
scope
>
</
dependency
>
<
dependency
>
<
groupid
>net.sourceforge.htmlunit</
groupId
>
<
artifactid
>htmlunit</
artifactId
>
<
version
>2.11</
version
>
<
scope
>test</
scope
>
</
dependency
>
- Write your tests. This is one example.
123456789101112@Test
public
void
notFound()
throws
IOException {
webClient.getOptions().setThrowExceptionOnFailingStatusCode(
false
);
webClient.getOptions().setPrintContentOnFailingStatusCode(
false
);
final
WebResponse response = page.getWebResponse();
assertEquals(
404
, response.getStatusCode());
assertEquals(
"abc.png was not found."
, response.getStatusMessage());
webClient.getOptions().setThrowExceptionOnFailingStatusCode(
true
);
webClient.getOptions().setPrintContentOnFailingStatusCode(
true
);
webClient.closeAllWindows();
}
- Execute
mvn:install
.
Conclusion
You should see the GlassFish server start, deploy your application, execute the unit tests, undeploy the application, and shutdown gracefully.There is a complete project on Bitbucket which demonstrates the entire process from start to finish doing a complete set of integration tests using HtmlUnit.
The project was developed using NetBeans 7.2.1, and GlassFish 3.1.2.
The complete project is located here: image-servlet.
0 comments :
Post a Comment