|
Frequently Asked Questions (FAQ)
# General, Technical
General Questions
What is 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.
Why another testing framework?
Even with tools like Cactus, JUnitEE and HttpUnit, testing Java Servlets 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 separation of concerns, 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.
What are JSP custom tags?
JSP tag extensions (informally known as custom tags) are reusable components that encapsulate functionality for use for page authors. For example, you might have small pieces of recurring logic within your JSP-based web application. In the past, such functionality would be written using scriptlets – small pieces of Java code embedded into the page. While this works and it achieves the desired result, embedding Java code can lead to maintainability problems in the future, especially if a “copy and paste” style of reuse has been adopted. In addition to this, embedding Java code within JSP pages makes the job of web designers (the people that are responsible for the look and feel) more difficult since they have to battle with source code that they may not understand.
Custom tags are a great way for wrapping up common and recurring functionality and reusing it throughout the pages of web applications. This additionally increases the maintainability and readability of the pages. Further information about custom tags can be found at http://java.sun.com/products/jsp/taglibraries.html.
Why test custom tags?
Testing any code that you write is important since it finds bugs and helps ensure that the quality of the delivered system is kept high. Since custom tags are components that can be reused within many web applications, it makes sense to ensure that they are as bug free as possible.
In addition to this, page authors (the people that will use your tags) will often try to use your tags in weird and wonderful ways that you had never imagined. This is another reason why testing tags is important, particularly if you are developing them for use by other people.
What is unit testing?
Unit testing is the process of testing a particular piece of code, or class in isolation. Since tags are always run within a JSP container, you could say that testing tags isn't strictly unit testing. However, from a testing perspective, you can think of a tag as a unit that should be tested in isolation.
Why test tags in isolation?
Custom tags should be tested away from the application specific pages on which they will ultimately be used, in the same way that we test Java classes individually with JUnit. This helps to assert that they perform as expected and also provides a more reliable way to test that they can be reused elsewhere in the future, be it on different pages or different web applications.
How can you test tags?
There are several ways to test custom tags. One of these simply involves testing the JSP page on which the tags is used, and verifying that the generated content of the page as a whole is as expected. This is generally known as functional or acceptance testing.
Alternatively, you can test the tag classes locally by mocking out the classes used by the Servlet/JSP APIs and simply calling the methods on the tag handler classes with a framework such as JUnit or Cactus.
What is different about TagUnit?
The motivation behind the TagUnit framework is to treat tags as the units from which JSP pages are built, meaning that the tags should be tested themselves, in isolation from the application specific pages on which they will ultimately be used. Basically, TagUnit treats custom tags as components rather than simple Java classes.
How does TagUnit work?
Since tags are dependent on the features provided by the JSP container in which they run, it makes sense to test them in this way - inside the container. This differs from the approach taken by mocking out the dependent classes in that the tags are tested inside their natural environment.
One of the advantages of using mock objects is that you are no longer dependent on the specifics of the container that you are using and therefore you can concentrate on the tags themselves rather than their interactions with the container. However, there are some benefits to be had by testing inside the container.
Why test inside the container?
Testing inside the container provides several benefits. First of all, although the individual tag handler methods can be tested as a part of the unit testing process, this still doesn't guarantee that they will work inside of the container when deployed.
In addition to this, tags have several side effects that are much more easily tested when they are running inside a container:
- introduction of scripting variables
- manipulation of page context attributes
- iteration and manipulation of body content
- manipulation of cookies
Having a suite of in-container tests means that you can take those tests and run them inside another container to check that you aren't relying on container specific features. Of course, on the flip side, it does mean that you can test such features if you need to make use of them.
Finally, the way that custom tags are managed by the container can vary from vendor from vendor. The JSP specification states that containers can pool/re-use tag handler instances and this does have an impact on how tag handlers should be written to be portable. Having an in-container test suite makes this particularly easy to assert.
Technical Questions
When I call the JSP containing my tests, why don't they run?
If, when requesting your test JSPs, you get an error message saying All tests should be run through the controller servlet - please see the documentation for more details, this means that you need to call your tests using the TagUnit controller servlet. For example, if your tests are located at /index.jsp , then instead of directly accessing index.jsp, you should request /test/servlet/RunTests?uri=index.jsp .
|