I ran a serious after-hours hack-a-thon on Sunday this past weekend. The result of which is now a Sourceforge project named JspTest. Here's the short description from the Sourceforge summary page:
JspTest is a JUnit extension for testing JavaServer Pages (JSP) outside a J2EE container. Internally, it uses the Jasper JSP compiler from the Jakarta Tomcat project and the Java compiler distributed as part of the system's default JDK.
We're talking about real unit testing here, not integration testing against code deployed on a web container like is the case with many web-related "xUnit" libraries.
In other words, it lets you write unit tests for your JSP pages like so:
import net.sf.jsptest.JasperTestCase;
/**
* Unit testing JavaServer Pages has never been this easy!
*/
public class HelloJspTest extends JasperTestCase {
protected String getWebRoot() {
return "./websrc";
}
public void testRenderingIndexJsp() throws Exception {
get("/index.jsp");
assertPageContains("Hello from Jasper");
}
public void testRequestAttributes() throws Exception {
setRequestAttribute("who", "You");
get("/say_hello.jsp");
assertPageContains("Hello, You!");
}
}
The good news is that there's no servers to start up (not even lightweight ones) and once a given JSP source file has been compiled (seems to take around 1 sec per file), further tests rendering that page with different request attributes etc. will execute blazing fast--just as if you were invoking a Servlet class directly (because you are!).
It's alpha and needs a whole lot of improvement (not least in terms of the API and extension points, not to mention missing built-in assertions) but it works alright for what I've used it on. What's in version control right now is based on a spike I did (I actually named the original Eclipse project as "JasperSpike") at figuring out how to use Jasper. As a result, the class hierarchy will likely evolve quite a bit in the near future. Also, I've only got a couple of acceptance tests in place because I was mostly just hacking to get Jasper's peculiarities sorted out for myself. That'll have to change if this thing takes on a life of its own.
There's no release, yet, (not even a development snapshot) so if you'd like to take a closer look you'll have to resort to doing a...
svn export https://svn.sourceforge.net/svnroot/jsptest/trunk jsptest-snapshot
...and (because I haven't yet written a build script) importing the "jsptest-snapshot" directory into Eclipse in order to export a .jar file.
PS. I am aware of the JSPUnit project at Sourceforge and that the first Google hit for "jsptest" is an existing open source library with the same name as my stuff. The thing is, both of these are effectively dead and haven't seen any real activity in years. They're basically inferior implementations of a subset of what projects like HttpUnit, jWebUnit, HtmlUnit, and Canoo WebTest offer to thousands of users worldwide.







