It's astonishing how such a difficult-to-find piece of code can be so short. I'd been flirting with the idea of doing true unit tests for JSPs for a long time, years perhaps.
Here are a few sites that helped me on my quest:
- Cactus
- CodeRanch
- Jetty's ServletTester
- HTTPUnit's ServletUnit
- TagUnit
- Unit Testing jsp:include
- JSFUnit
- MockRunner
- HtmlUnit
- Selenium
- JWebUnit
- HTTPUnit
- Maven JSP Compiler support
- Jetty's ServletTester
JSPUnit looks usable, but I want something closer to a "real" servet environment.
So, knowing how configurable Jetty is generally, and knowing how fast it is even when running as a full-blown container, I decided to see whether I could get Jetty's ServletTester to do the job. This site and this site present more usages of ServletTester. This code pointed me in the direction of JspServlet.
I won't bore you with the different combinations of configuration I tried, but the result is presented here as a minimalist Hello World maven war project.
Of course, after I'd put this together, and while googling to flesh out this article, I found JspTest at Sourceforge. Needless to say, it does the same stuff. It seems to be using its own implementation of the Servlet API, so perhaps it's further from a real container than I'd like. Some day I might go to the bother of trying both approaches against some truly horrible JSPs.
What I'd like, and this helps a bit, is true code coverage, à la Clover/Cobertura/Emma. I imagine it wouldn't be too hard to do code coverage against the generated servlet; but how to report that against the original JSP, preferably with pretty colours?