Your test case class should extend ServletTestCase
whenever you are unit testing:
HttpServletRequest
, ...)
setUp()
,
testXXX()
and tearDown()
methods as
instance variables of the ServletTestCase
class (and thus
as instance variables of your test case class as it extends
ServletTestCase
).
Instance variable name |
request
|
Class name |
org.apache.cactus.server.HttpServletRequestWrapper
, which inherits from
javax.servlet.http.HttpServletRequest
|
Cactus wraps methods of the original HTTP request in order to return
the HTTP related parameters set up in the beginXXX()
method. Thus, you will be able to pass this request object to
your code to test and set the needed parameter in the
beginXXX()
method.
For example, if your code under test calls
getCookies()
on the request object that you have
passed to it, it will return the cookies that you have added to the
HTTP request in beginXXX()
by calling the
WebRequest.addCookie()
method.
See the javadoc for the
org.apache.cactus.WebRequest
interface and the
org.apache.cactus.server.HttpServletRequestWrapper
class for all details. You should also look at the
samples provided in the Cactus distribution.
Cactus provides some additional methods to ease writing tests (see the javadoc for full details). These methods are provided because it is not easy (if not downright impossible in some cases) to simulate them with real configuration data:
setRemoteIPAddress()
: sets the remote IP address
that will be returned by getRemoteIPAddress()
,
setRemoteHostName()
: sets the remote Host name
that will be returned by getRemoteHostName()
,
setRemoteUser()
: sets the remote user name
that will be returned by getRemoteUser()
.
Instance variable name |
response
|
Class name |
javax.servlet.http.HttpServletResponse
|
Instance variable name |
config
|
Class name |
org.apache.cactus.server.ServletConfigWrapper
, which inherits from
javax.servlet.ServletConfig
|
web.xml
, etc...,
config
implicit object will contain all
initialisation parameters defined in web.xml
under the Servlet Redirector servlet definition.
See the javadoc for the
org.apache.cactus.server.ServletConfigWrapper
class for all details. You should also look at the
samples provided in the Cactus distribution.
setInitParameter()
: sets an initialisation
parameter (as if it has been defined in the web.xml
file),
setServletName()
: sets the Servlet name that will be
returned by getServletName()
(if not set, the
Cactus Servlet redirector name will be returned).
config.getServletContext()
.
However, Cactus wraps the ServletContext
in a
ServletContextWrapper
in order to take into account
simulated URLs and provide additional methods to help write tests.
See the javadoc for the
org.apache.cactus.server.ServletContextWrapper
class for all details. You should also look at the
samples provided in the Cactus distribution.
Additional methods provided:
getLogs()
: returns the text that has been logged
by calls to ServletContext.log()
methods. This is
a helper method that makes it easy to assert what is logged.
setInitParameter()
: sets an initialisation
parameter (as if it has been defined in the web.xml
file, using the <context-param>
element).
Instance variable name |
session
|
Class name |
javax.servlet.http.HttpSession
|
WebRequest.setAutomaticSession(false)
method (it is
true
by default). This could be useful for cases
where your code under test verifies branches when
"request.getSession(false)
" is null
.
javax.servlet.GenericServlet
(these are
the log()
, getServletConfig()
, ...
methods), then you need to call the init(ServletConfig)
method of your servlet to initialise its internal
ServletConfig
object.
For example:
public void testXXX() { MyServletToTest servlet = new MyServletToTest(); servlet.init(config); // Call a method to test that uses a method inherited from Generic Servlet servlet.someMethodToTest(); [...] }
See the samples provided as part of the Cactus distribution.
This is a very basic sample intended to give you a flavour of Servlet unit testing. Check the distribution samples for extensive examples.
WebRequest
and WebResponse
objects.
public void beginXXX(WebRequest theRequest) { // Set up HTTP related parameters theRequest.setURL("jakarta.apache.org", "/mywebapp", "/test/test.jsp", null, null); theRequest.addCookie("cookiename", "cookievalue"); } public void testXXX() { MyServletToTest servlet = new MyServletToTest(); servlet.init(config); // Call method to test servlet.methodToTest(); // Perform some server side asserts assertEquals("someValue", session.getAttribute("someAttribute")); assertEquals("jakarta.apache.org", request.getServerName()); } public void endXXX(WebResponse theResponse) { // Asserts the returned HTTP response Cookie cookie = theResponse.getCookie("someCookie"); assertEquals("someValue2", cookie.getValue()); assertEquals("some content here", theResponse.getText()); }