Tagit allows custom JSP tags to be tested with a container. It is very different than TagUnit; the two libraries complement each other nicely. Tagit is an implementation of the Mock Objects testing pattern.

All custom JSP tags generate HTML, and the HTML can be validated. Often custom tags are reading data from the HTTP request or the HTTP session, Tagit allows data to be stored in the session or the request. TagIt cannot be used for any custom tags that generate data in the request etc; Tagit is designed to only tags that simplify the creation of HTML.

Here is some sample unit test code:

package net.scholnick.unittests;

import java.util.*;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import junit.framework.TestCase;

import net.scholnick.net.test.tag.UnOrderedListCollectionTag;
import net.scholnick.tagit.mock.MockPageContext;

 * UnOrderedListCollectionTagTestCase demonstrates the usage of the tagit library
 * to run out of the container unit tests against custom tag classes.
 * @author Steven Scholnick <steve@scholnick.net>
public class UnOrderedListCollectionTagTestCase extends TestCase
	public void testTag() throws JspException
		// Create the mock page context
		PageContext context = new MockPageContext();
		List data = Arrays.asList( new String[] { "one", "two" });

		// Save the data in the request
		context.getRequest().setAttribute( "data", data );

		// create the tag
		UnOrderedListCollectionTag tag = new UnOrderedListCollectionTag();

		// call the set methods that would be called automatically
		tag.setName( "data" );
		tag.setPageContext( context );

		// run the tag by calling doStartTag

		// now check the page context's Out writer against the expected HTML data
		assertEquals( "<ul><li>one</li><li>two</li></ul>", context.getOut().toString() );

TagIt is licensed under the MIT, except for the following packages that are released into public domain:

  • net.scholnick.net.test.tag
  • net.scholnick.unittests
Any classes in these packages can be used for any purpose at anytime. They demonstrate how to use TagIt.

Download the latest version. All source code is included in the download. A library JAR is also included in the download that can be added to your CLASSPATH.