
























|
 |  |  |  | Tutorial for creating a J2EEUnit Test Case |  |  |  |  |
This section explains how to write a test case, using J2EEUnit.
 |  |  |  | Step 2 : Extend ServletTestCase or JspTestCase |  |  |  |  |
 |  |  |  | Step 3 : Standard JUnit methods |  |  |  |  |
 |  |  |  | Step 5 : Signature of the test methods |  |  |  |  |
This is where it is a bit different from JUnit ! For each test
XXX that you want to implement you can define 3 methods
(2 being optionals) :
-
void beginXXX(ServletTestRequest) : This method
is optional and should be used to set up some HTTP-request related
parameters before your test case is executed. Whereas the setUp()
method is global for all test cases, this method is only executed
before the test case of the same name (i.e. testXXX() ).
This method is executed on the client side.
The ServletTestRequest object passed to the beginXXX()
method contains all the methods needed to set up the HTTP parameters.
See the javadoc for
ServletTestRequest
for detailed information on the API. Here is a short summary :
ServletTestRequest
|
addCookie(String, String)
|
Adds a cookie to the list of cookies that will be available
in testXXX() , setUp() and tearDown()
using the HttpServletRequest.getCookies() API
|
addHeader(String, String)
|
Adds a HTTP header to the list of headers that will be available
in testXXX() , setUp()
and tearDown() using the HttpServletRequest.getHeader()
API.
|
addParameter(String, String)
|
Adds a HTTP parameter to the list of parameters that will be
available in testXXX() , setUp() and
tearDown() using the HttpServletRequest.getParameter()
API.
|
setAutomaticSession(boolean)
|
If false , no HTTP session
wil be automatically created (i.e. the implicit session
object will be null . See section 6 below for implicit
objects). By default the value is true .
|
setURL(String, String, String, String, String)
|
If used, simulates a URL. From the point of view of your
test case, it is as if your servlet were called by this URL
instead of the Redirector servlet URL (see
configuration).
The parameters are : server name (+ port number), context path,
servlet path, path info, query string.
|
-
void testXXX() : This is the test method. In it,
you will instantiate the class to test, call the method method
and write the checking to verify that the test was successful
using JUnit asserts(..) , assertEquals(...) ,
fail(...) , etc methods.
-
void endXXX(HttpURLConnection) : This method is
optional and should be used to test HTTP-response related values
that might have been set by the tested method. For example, you
might want to verify that a cookie was sent back to the client,
that a HTTP header has been correctly set, the returned servlet
output stream, etc. This method is executed on the client side.
J2EEUnit provides helper methods in the j2eeunit.util
package (download the
J2EEUnit sample application
for example on how to use them) :
-
AssertUtils.getCookies() : retrieves all cookies sent
back from the server side,
-
AssertUtils.getResponseAsString() which returns the
server output stream as a string that you can assert.
The HttpURLConnection object passed to this method
is the standard java.net.HttpURLConnection . Thus you
have access to all the standard API (getHeaderField(), ... )
|
 |  |  |  | Step 6 : Implicit objects |  |  |  |  |
Depending on whether your test class extended
ServletTestCase or JspTestCase you have
access to different server-side objects.
 |  |  |  | Implicit objects provided by ServletTestCase |  |  |  |  |
The following server-side objects are made available to your testXXX(),
setUp() and tearDown() methods :
-
request : This is the HttpServletRequest
object. Actually, it is a modified HttpServletRequest
that overrides some of the original HttpServletRequest
methods in order to return the parts of the simulated URL that
may have been set up in the beginXXX() method using
the ServletTestRequest.setURL() method. The overridden
methods are :
-
HttpServletRequest.getServerName() ,
-
HttpServletRequest.getServerPort() ,
-
HttpServletRequest.getRequestURI() ,
-
HttpServletRequest.getContextPath() ,
-
HttpServletRequest.getServletPath() ,
-
HttpServletRequest.getQueryString() ,
-
HttpServletRequest.getPathInfo()
-
response : This is the HttpServletResponse
object that will be used to return the HTTP response to the client.
-
session : This is the HttpSession
object. It is created only if the automatic session creation is
set to true (it is set using the ServletTestRequest.setAutomaticSession(boolean) ),
which is the default. If you have set it to false
then the session's value is null .
-
config : This is a ServletConfig object.
It is the configuration that comes from the Redirector Servlet. Thus
if you want to test a method that needs a ServletConfig
object to retrieve some static configuration values, you should
define these values in the web.xml mapping section for
the Redirector Servlet.
|
|
|
|
|