webapplicationtestcase
Class WebApplicationTestCase

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by webapplicationtestcase.WebApplicationTestCase
All Implemented Interfaces:
junit.framework.Test

public abstract class WebApplicationTestCase
extends junit.framework.TestCase

This class provides the simplest web application integration testing available. The idea behind this test is that you send some request parameters to a web application, via post or get, and then analyze the response. It is taken directly from the Ruby on Rails test framework by David Heinemeier Hansson -- I have just implemented it in Java. More information on what I have done can be had from the article "A Guide to Testing the Rails.

Version:
1.0
Author:
Jason Edwards

Constructor Summary
WebApplicationTestCase()
           
 
Method Summary
 void assertNoTag(java.lang.String tagName, java.util.Map attributes, java.lang.String text)
          Evaluates the contents of the response to determine no tag meets the criteria specified.
 void assertRedirectedToAction(java.lang.String action)
          Evaluates the response was a redirect to the specified action.
 void assertResponse_Error()
          Evaluates the http status is an error (5xx).
 void assertResponse_Missing()
          Evaluates the http status is missing (404).
 void assertResponse_Redirect()
          Evaluates the http status is a redirect (3xx).
 void assertResponse_Success()
          Evaluates the http response status is success (200).
 void assertResponse(int responseCode)
          Evaluates the http status.
 void assertTag(java.lang.String tagName, java.util.Map attributes, java.lang.String text)
          Evaluates the contents of the response to determine if a tag meets the criteria specified.
 void get(java.lang.String action, java.util.Map requestParameters)
          Sends a request to the specified action as a "get".
protected  HttpResponse getHttpResponse()
          Accessor for the most recent HttpResponse for the current WebApplicatoinSession.
protected abstract  java.lang.String getWebApplicationBaseUrl()
          This is the base url of the web application to test.
 void post(java.lang.String action, java.util.Map requestParameters)
          Sends a request to the specified action as a "post".
protected  void setUp()
           
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runBare, runTest, setName, tearDown, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

WebApplicationTestCase

public WebApplicationTestCase()
Method Detail

setUp

protected void setUp()
              throws java.lang.Exception
Overrides:
setUp in class junit.framework.TestCase
Throws:
java.lang.Exception

getWebApplicationBaseUrl

protected abstract java.lang.String getWebApplicationBaseUrl()
This is the base url of the web application to test. For example, if you want to test the page "http://localhost:8080/myappname/index.html", then the base url would be "http://localhost:8080/myappname", and you can call get("index.html", null, null); to request the "index.html" page.

This basically allows you to follow the do not repeat yourself (DRY) principle and specify the base url for your web application in a single spot.

Returns:
the base url of the web application to test

getHttpResponse

protected HttpResponse getHttpResponse()
Accessor for the most recent HttpResponse for the current WebApplicatoinSession.

Returns:
the most recent HttpResponse for the current WebApplicationSession.

post

public void post(java.lang.String action,
                 java.util.Map requestParameters)
Sends a request to the specified action as a "post". NOTE: The request parameters should not be URL encoded, the WebApplicationSession will handle that.

Parameters:
action - a String containing the name of the action the request should be sent to.
requestParameters - a java.util.Map of the request parameters to be passed to the action.

get

public void get(java.lang.String action,
                java.util.Map requestParameters)
Sends a request to the specified action as a "get". NOTE: The request parameters should not be URL encoded, the WebApplicationSession will handle that.

Parameters:
action - a String containing the name of the action the request should be sent to.
requestParameters - a java.util.Map of the request parameters to be passed to the action.

assertResponse_Success

public void assertResponse_Success()
Evaluates the http response status is success (200).


assertResponse_Redirect

public void assertResponse_Redirect()
Evaluates the http status is a redirect (3xx).


assertResponse_Missing

public void assertResponse_Missing()
Evaluates the http status is missing (404).


assertResponse_Error

public void assertResponse_Error()
Evaluates the http status is an error (5xx).


assertResponse

public void assertResponse(int responseCode)
Evaluates the http status.

Parameters:
responseCode - the expected http response code

assertTag

public void assertTag(java.lang.String tagName,
                      java.util.Map attributes,
                      java.lang.String text)
Evaluates the contents of the response to determine if a tag meets the criteria specified.

Parameters:
tagName - The name of the tag to find, e.g. div, span, a
attributes - Attributes of the tag to find, e.g. id="errorMessage" or href="login.html"
text - The text to find in the content.
See Also:
WebApplicationAssertions.assertTag(HttpResponse, String, java.util.Map, String)

assertNoTag

public void assertNoTag(java.lang.String tagName,
                        java.util.Map attributes,
                        java.lang.String text)
Evaluates the contents of the response to determine no tag meets the criteria specified.

Parameters:
tagName - The name of the tag to find, e.g. div, span, a
attributes - Attributes of the tag to find, e.g. id="errorMessage" or href="login.html"
text - The text to find in the content.
See Also:
WebApplicationAssertions.assertNoTag(HttpResponse, String, java.util.Map, String)

assertRedirectedToAction

public void assertRedirectedToAction(java.lang.String action)
Evaluates the response was a redirect to the specified action.

Parameters:
action - The name of the action you are expecting to be redirected to
See Also:
WebApplicationAssertions.assertRedirectedToAction(HttpResponse, String, String)