Overview
WebApplicationTestCase is an extension for JUnit. It was heavily influenced by the functional tests David Heinemeier Hansson created for Ruby on Rails (read this article for more on testing in Rails). Due to limitations in the Java programming language and J2EE architecture, WebApplicationTestCase tests aren't quite as elegant as they are in Rails. For example, in Rails a test would look like this:
def test_find_widget get(:widget, {:id => '2'}) assert_not_nil(assigns['widget']) assert_equal(1, assigns['widget'].id) assert_response(:success) assert_template('widget') assert_tag('html') end
Here's a WebApplicationTestCase test which does essentially the same thing:
public void testFindWidget { HashMap params = new HashMap(); params.put("id", "2"); get("widget.html", params); assertResponse_Success(); assertTag("html", null, null); }
I haven't figured out how to replicate the assert_template()
method, or the assigns
and session
hashes. I have some ideas, but they are all far more complicated than what I've done here so far.
Downloading WebApplicationTestCase
The first release is 1.0. You can download it from SourceForge here. If you want to browse the Subversion repository, go here.
I'm using Subversion slightly differently than most projects do. To check out the project do this:
svn export https://webapptestcase.svn.sourceforge.net/svnroot/webapptestcase/trunk/WebApplicationTestCase
As you can see I created a directory under the trunk, so you don't *have* to create an alias, you still can if you would like.
Once you have downloaded the project, you can build the jar file like this:
$ ant
If you want to build the javadocs, do this:
$ ant docs
I don't have a Windows computer, so I haven't tried the build on Windows, though I can't imagine why it wouldn't work.
Getting Started
So what does a test look like when using WebApplicationTestCase? It's about as simple as I can imagine:
1 public class MyWebAppTest extends WebApplicationTestCase { 2 protected String getWebApplicationBaseUrl() { 3 return "http://localhost:8080/my-web-app"; 4 } 5 6 public void testLogin() { 7 get("login.html", null); 8 assertResponse_Success(); 9 10 HashMap loginParams = new HashMap(); 11 loginParams.put("username", "foo"); 12 loginParams.put("password", "bar"); 13 post("login.html", loginParams); 14 assertResponse_Redirect(); 15 assertRedirectedToAction("index.html"); 16 } 17 }
Basically, the main idea is you send a request, along with a Map of params, to the application, and then check
the response. Line #3 tells the test where to send the requests, so on line #7, a get request will be sent to
"http://localhost:8080/my-web-app/login.html"
.
I have created the following assertions:
assertResponse_Success()
(javadoc)assertResponse_Redirect()
(javadoc)assertResponse_Missing()
(javadoc)assertResponse_Error()
(javadoc)assertResponse(int expectedResponseCode)
(javadoc)assertRedirectedToAction(String expectedAction)
(javadoc)assertTag(String tagName, Map attributes, String text)
(javadoc)assertNoTag(String tagName, Map attributes, String text)
(javadoc)
I'm sure there could be many more.