Matt RaibleMatt Raible is a Web Architecture Consultant specializing in open source frameworks.

10+ YEARS


Over 10 years ago, I wrote my first blog post. Since then, I've authored books, had kids, traveled the world, found Trish and blogged about it all.

What's the best way to test Tag Libraries?

I know of two tag libraries that are in dire need of unit tests - the displaytag and struts-menu. Both have no tests. I've looked briefly at TagUnit, but aren't you just writing JSPs (with custom tags) to test JSPs? I'd rather have an Ant/JUnit driven solution. Also, are there HTML versions of the user guides for TagUnit (Simon ;-)? I hate PDFs.

So my question is - how do you test your tag libraries?

Posted in Java at Oct 07 2003, 10:28:26 AM MDT 7 Comments
Comments:

I use cactus. It works really well, is built on junit and integrates with ant.

Posted by Jason Arndt on October 07, 2003 at 12:06 PM MDT #

[Trackback]

The question is how to test JSP custom tags, and one answer is TagUnit.

Posted by Simon Brown's weblog on October 07, 2003 at 01:09 PM MDT #

If you're <em>really</em> annoyed at PDF's, get someone to make them into HTML for you. I've (mis)used Google for this before now. Just search for the PDF in Google and then use their 'view as html' option. For the TagUnit PDF, I typed in "site:www.tagunit.org tagunit-userguide.pdf" which seemed to do the trick. (not checked it too hard, though!)

Posted by Rick Lawson on October 07, 2003 at 02:09 PM MDT #

Rick, that is one of the funniest and yet best ideas I have heard in a while! :-) The honest answer to the PDf thing is that like writing docs in Word because that's generally what it's good at. Also, since I have a Mac, generating PDFs is easy, hence the PDF docs. I also prefer PDFs for long documents because you can print them out easily, two pages to a sheet of A4/letter if necessary.

Posted by Simon Brown on October 07, 2003 at 02:37 PM MDT #

I am a fan of mock objects as opposed to in-container testing. You want your unit tests to finish as fast as possible or else you will not use them.

For tag libraries you'd probably start by creating a mock object of PageContext. It may be necessary to refactor the actual tag code a little, since the tag may use functionality from superclasses, but that is never a bad thing. Unit tested code always looks different from non tested code, and in most cases it is better.

Posted by Rhys Keepence on October 07, 2003 at 06:51 PM MDT #

Like Rhys, we use mock objects. For us, the effort was not worth the payoff to introduce another testing framework like Cactus or TagUnit. Mock objects make tests super fast. Currently, we have mocks for JspWriter, HttpServletRequest/Response, PageContext and HttpSession. Basically, we just flesh out dummy implementations of the methods used by the tags. All the rest throw RuntimeExceptions that say "Method not implemented yet" so if we forget to implement a method that is used, it will blow up loudly. Then we create expected results, compare them when actual results. Viola - unit tests for tags.

Posted by Ryan Breidenbach on October 09, 2003 at 08:49 AM MDT #

Following up from Ryhs' post, I've been using the TagTestHelper from J2EE Mock Objects jar. I blogged a little about it a while back: http://www.corvine.org/blog/archives/000024.html

Posted by Andy Marks on August 31, 2004 at 09:29 PM MDT #

Post a Comment:
  • HTML Syntax: Allowed