 |
How do you test tag libraries?
Posted by simongbrown on October 07, 2003 at 11:58 AM | Comments (6)
Matt is looking for a way to test tag libraries and rather than write a lengthy comment, I thought I'd follow it up here.
In the first part of his post, Matt says, ... I've looked briefly at TagUnit, but aren't you just writing JSPs (with custom tags) to test JSPs?
In answer to this question, yes, you're exactly rght in saying that you're just writing JSPs. This is the essence of TagUnit, and why I believe it's a better way to test tags than using something like Cactus. Sure, you can test the various classes with JUnit, but since you don't use the classes in this way from a JSP page, this approach can only take you so far. JSP tags are reusable components and I think that they should be tested as such. For me, this means testing those tags in the same way that they will be used on a JSP page, and that's by writing the tests and assertions as JSP pages.
Doing this has several benefits. First of all, you don't have to start mocking out parts of the servlet specification just to test the tag handlers. Instead, you're running the tests inside a real server. If you've looked into the JSP specification, you'll know that the tag lifecycle is a tricky business because of the way that JSP containers can optionally reuse and pool tag handler instances. By writing your tests as JSP pages, you instantly get all this framework for free, meaning that you can accurately test the behaviour on your desired target platform. Also, if you're aiming for cross-container compatibility, this is as simple as (in theory!) just dropping the WAR file containing the tests into a new container.
Matt wants an Ant/JUnit solution and I can see why this is desirable. I'm far more likely to run the tests if I don't have to keep starting up the JSP container. Unfortunately, there aren't many ways around this without exploring avenues such as mock objects. However, TagUnit does provide an Ant task that can be used to initiate and collect the results of running the JSP-based tests. The basics of this are documented, although this document is being expanded as we speak ... isn't it Sam? ;-) As for whether there are HTML versions of the docs ... not yet, but there might be one day.
I should probably add a disclaimer at this point, and highlight the fact that I created the TagUnit project. Clearly I'm biased, but I do think that it provides an improved way to test custom tags. Whether it's better ... that's for you to decide.
Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
How not to do it.
A couple years ago I was faced with writing tests for my custom tags. The current solution in use was to document what to expect from a set of JSPs in some of the most tedious, error prone and worthless Word documents I've ever seen.
My solution was to write a simple JSP for each test. A controller JSP set up the environment and forwarded to a specified JSP. Ant ran a series of Tests (not TestCases) that connected to the web server for each JSP and compared the result to a plain text file (with some alterations to strip out ID numbers).
It worked, but was difficult to handle all those files (two per test). There was a suggestion of bundling the files together, but that never got implemented. When I saw TagUnit a few months later it was a "why the hell didn't I think of that?" moment.
Posted by: tackline on October 07, 2003 at 06:15 PM
-
No silver bullet for tag testing
TagUnit looks good, but can only take you so far. Checking for content of the generated tag for example will be very hard with very dynamic content (for example a tag I wrote a while back to return formatted news via RSS). By specifiying a local RSS file I could test the return of the tag, however wouldn't then be testing the ability to load the file via the web. When all is said and done, the visual apperance of the tag (if any) and the return of highly dynamic data will have to be checked by hand/eye. If you are in the mood for some ranting on the subject of testing HTML output, you might be interested in this post over at the Bile Blog: http://www.jroller.com/page/fate/20031008#tdd_obsession
Posted by: samnewman on October 09, 2003 at 08:45 AM
-
No silver bullet for tag testing
I agree that TagUnit can only take you so far, but your refined RSS tag is a perfect candidate for testing. If you treat your tag as a black box, all that you want it to do is read an RSS feed from a URL and place some objects into the PageContext. To test it, place a sample RSS feed inside the testing web application and specify it's URL (e.g. http://localhost:8080/test/samplerss.xml). Now, once your tag has put the information into page scope, you can use TagUnit's assertPageContextAttribute tag to test that the objects are present, and that they have the appropriate values. Generally, embedding large amounts of HTML inside tag handlers is bad - it's unmaintainable and it cripples reuse. It's also hard to test, and if your tags are doing this, then I would recommend refactoring them because presentation/formatting/markup should ideally be in the JSPs. Tags should ideally just perform actions, and it's those actions that should be tested. I agree that testing large amounts of generated content is a pain and this is exactly what Hani is getting at when testing web applications/sites, but it's a separate issue.
Posted by: simongbrown on October 10, 2003 at 01:58 AM
-
Is is possible to write unit tests for Tags, and it is quite easy when using the org.springframework.mock.web package (spring-mock.jar, distributed with Spring 2.0). It provides mock objects for PageContext and both servlet and portlet request/reponse objects.
The MockPageContext does not support getOut():JspWriter.
To test the output of your Tag, you might to refactor to some extend.
My solution is this: The output is produced by toString():String. toString() is called by doStartTag() or doEndTag() or render the output via pageContext.getOut() and is used in tests to check the result.
On a side note: I just noticed this blog entry is about 3 years old. But its ranked quite high when googling for "jsp tag libraries junit", therefore it might be good to know whats possible today.
Posted by: enchos on August 30, 2006 at 06:26 AM
-
wow power leveling
wow powerleveling
wow power leveling
wow gold
wow items
feelingame.com
wow tips
Most Valuable WOW Power Leveling Service
wow power leveling faq
cheap wow power leveling
wow power leveling
wow powerleveling
wow power lvl
Posted by: wowleveling3 on December 13, 2007 at 06:08 PM
-
网络营销软件
网络营销软件
网络营销软件
群发软件
群发软件
---
群发软件
网络营销软件
论坛群发软件
网站排名软件
群发软件
推广小助手破解版
论坛群发软件
网站排名软件
群发软件
网络营销软件
网站推广软件
信息群发软件
论坛群发软件
信息群发软件
博客群发软件
qq群发软件
邮件群发软件
博客群建软件
企业名录搜索软件
信息群发软件
邮件群发软件
论坛群发软件
博客群发软件
网站推广软件
网络营销软件
全能营销破解版
Posted by: bininine on December 16, 2007 at 01:21 AM
|