Unit testing JSF based web applications has been considered difficult because of the constraints of testing JSF components outside the container. Most of the web-tier testing frameworks follow black-box testing approach where developers write test classes using the web components to verify the rendered HTML output is what is expected. Frameworks such as HtmlUnit, HttpUnit, Canoo WebTest, and Selenium fall into this category. The limitation of these frameworks is that they only test the client side of a web application.
But this trend is changing with the recently released JSFUnit and other JSF testing frameworks such as Shale Test and JSF Extensions that support white-box testing to test both client and server components of the web application. And projects like Eclipse Web Tools Platform (WTP) and JXInsight are also helping in the development and testing of JSF applications.
Like any other unit testing framework, a typical JSF test framework should provide the following features:
- Ability to run a test case in isolation, outside the container and from within an IDE like Eclipse or NetBeans to promote Test Driven Development (TDD).
- Ability to test full HTTP Request-Response lifecycle.
- Integration with build tools such as Ant or Maven to run unit tests in an automated build and Continuous Integration environment.
The test framework should also support the JSF specific requirements like:
- Use JSF API calls in the test methods instead of checking HTML output or client-side DOM object.
- Navigation testing by validating the user input on the form and access the proper page view.
- Verify that actions perform as expected and error conditions produce expected error messages.
- Validation of JSF components even when the "rendered" attribute is set to false (where the component is not rendered on the web browser but still has state on the server side).
- Verify that the XML configuration files (faces-config.xml etc) are loaded correctly.
JSFUnit, which is built on HttpUnit and Apache Cactus, allows integration testing and debugging of JSF applications and JSF AJAX components. It can be used for testing both client and server side JSF artifacts in the same test class. With JSFUnit API, the test class methods can submit data on a form and verify that managed beans are properly updated.
JSFUnit includes a JSF Static Analysis test suite to validate the JSF configuration files to identify any configuration related issues before they occur at run-time. It also provides JSFTimer component for performance analysis of the JSF lifecycle. This can be used to trace the response time for each phase of the JSF lifecycle for a given web request and assert that the response time is within SLA limits.
JSFUnit includes support for RichFaces and Ajax4jsf components. Beta 1 version of this framework was released last month and the second Beta Version release is scheduled for the end of next month.
Shale Test Framework, an Apache project, provides mock object implementations for Servlet and JSF components. These base test classes can be used for testing Shale framework classes as well as JSF application components built on top of the framework. Shale Test can be used to write isolated unit tests that exercise the methods exposed by ViewController interface. This is useful because of the complexity involved in setting up the runtime environment for a ViewController component. Following are the main classes in Shale Test framework:
- AbstractJsfTestCase: This is the base test class for unit tests for Servlet and JSF components in a web application.
- AbstractViewControllerTestCase: This class, an extension of AbstractJsfTestCase, provides utility methods needed to test the common scenarios when using ViewController implementation classes.
- There are also mock implementations of other JSF web components, namely MockApplication, MockExternalContext, and MockFacesContext.
Shale Test also has integration with Spring Framework to allow a JSF component to be bound as a managed bean or as a Spring bean.
JSF Extensions project is a collection of non-UI components that extend the functionality of a JSF 1.2 based web application. It uses JUnit, HTMLUnit, and Cactus for automated testing. Test-Time module of the framework provides mock objects for JSF 1.2 components. Design-Time module has a JSF hot-deploy feature to stop and restart the application without going through build & redeploy cycles. This feature, based on JSF PhaseListener interface, causes all the configuration files and classes to be reloaded. This allows the developers to follow a test driven iterative JSF development approach when writing JSF applications.
ToolsThere are also some development tools that aid in the development and performance monitoring of JSF applications. JXInsight from JInspired group provides a JSF trace and profile feature which is based on the PhaseListener interface. By including this tracing phase listener in the JSF application, developers and web administrators can collect information such as trace stacks, java call stacks and runtime UIComponent hierarchy associated with a JSF request.
JSF tools sub-project in Eclipse WTP project also provides tool support for developing and deploying JSF based web applications with features such as Visual JSF Page Editor and Faces Config Editor.