From the TagUnit
In the same way that JUnit allows us to write unit tests for Java classes, TagUnit
allows us to unit test JSP custom tags, inside the container. In essence, TagUnit
is a tag library for testing custom tags within JSP pages.
Even with tools like JakartaCactus
, testing JavaServlets
and JSP pages is hard, particularly if they contain specific business or presentation logic that needs to be tested. Best practices around J2EE development suggest that logic should be encapsulated in JavaBeans
or JSP custom tags for better SeparationOfConcerns
, maintainability, reusability and to facilitate easier testing. Although JUnit can be used to test JavaBeans
, testing custom tags by simply invoking their methods doesn't make sense. Custom tags are components and therefore need to be tested at that level, in the way that they would normally be used from within a JSP page.
provides a way to perform assertions on the content that custom tags generate and the side-effects that they have on the environment such as the introduction of scoped (request/page/session/application) attributes, cookies and so on. In addition to this, assertions can be made on the constraints specified within the tag library descriptor file that give us a way to verify the contract that a tag provides. In just a four line JSP page, TagUnit
can automatically perform tests such as asserting whether the tag handler class is loadable and that it has setter methods for all declared attributes. To supplement this, user defined tests provide a way to perform assertions on the description of a tag, such as its body content and the details of any attributes.