Getting Started
User Guide
TagUnit with Ant
Project Home
Mailing Lists
Bugs & features
Browse CVS
SourceForge.net Logo


To illustrate the types of testing that can be performed with TagUnit, here are some examples taken from the tagunit-examples web application (included with the downloadable distribution).

Testing the basics of a tag library

This short fragment shows how easy it is to start testing your own tag library. Here, the TagUnit tags are used to define a test package for the JSP Standard Tag Library. The outer tags demarcate the tests, while the inner tag specifies the location of the TLD file for the tag library. Even without defining any more tests, this simple fragment is sufficient to automatically test the declarative aspects of a tag library. In this example, TagUnit would look at each of the tags in the JSTL and determine whether:
  • the tag handler class is loadable, implements the Tag interface and can be instantiated
  • the TEI class (if applicable) is loadable, extends TagExtraInfo and can be instantiated
  • there are public setter methods for each and every attribute that a tag supports
Even this basic level of automated testing provides an additional level of confidence when (re)deploying a tag library.
  <tagunit:testTagLibrary uri="/jstl-core">
    <tagunit:tagLibraryDescriptor jar="standard.jar" name="c.tld"/>

Testing the specifics of a tag

Once the basics have been tested, the next step is to start writing assertions based upon the specifications of each tag. This is achieved by performing tests upon the constraints that are specified for that tag in the TLD file. The following JSP fragment shows how constraints for the JSTL <c:set> tag can be tested.
  <tagunit:assertBodyContent name="JSP"/>

  <tagunit:assertAttribute name="value" required="false" rtexprvalue="false"/>
  <tagunit:assertAttribute name="var" required="false" rtexprvalue="false"/>
  <tagunit:assertAttribute name="scope" required="false" rtexprvalue="false"/>
  <tagunit:assertAttribute name="target" required="false" rtexprvalue="false"/>
  <tagunit:assertAttribute name="property" required="false" rtexprvalue="false"/>

Performing these tests helps to ensure that the usage contract of a tag isn't unknowingly modified between releases.

Asserting generated body content

This fragment is from a JSP containing test cases for testing the body content generated from a tag. Here, the assertContent tag is simply used to compare body content that is generated under a set of scenarios.
  <tagunit:assertContent name="Simple loop">
    <tagunit:actualResult><c:forEach var="i" begin="1" end="3">x</c:forEach></tagunit:actualResult>

Asserting the value of a page context attribute

Often, custom tags interact with their environment (through the PageContext) and modify the values of attributes stored in the request/page/session/application. As this next example shows, performing assertions on such attributes is straightforward.
  <c:set var="myVar" scope="request" value="Hello World!"/>
  <tagunit:assertPageContextAttribute name="myVar" type="java.lang.String" scope="request" value="Hello World!"/>

Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the U. S. and other countries.