Pages

Saturday, January 05, 2013

Using GlassFish 3.1.1 Embedded with JUnit 4.x and HtmlUnit 2.x

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 solution

The 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

  1. First make sure that you configure the Maven plugin. Complete details can be found in Oracle GlassFish Server Embedded Server Guide
  2. Next, configure the surefire plugin to skip its normal test cycle.
  3. Add a dependencies for JUnit and HtmlUnit to the pom.xml
  4. Write your tests. This is one example.
  5. 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 :