![]() | ![]() |
Apache > Jakarta > Cactus > Writing Tests | Docs for: v1.7.2 | v1.7 Last update: August 30 2007 |
When to use?
Your test case class should extend
Provided Implicit Objects
Cactus automatically initializes the implicit objects for you and
they are made available to your
You may ask yourself how Cactus initializes these objects. The
mechanism is described in the How it
works guide.
The provided implicit objects are: request
Cactus wraps methods of the original HTTP request in order to return
the HTTP related parameters set up in the
For example, if your code under test calls
See the javadoc for the
Additional methodsCactus 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:
response
Cactus does not wrap the response.
config
Cactus wraps the original Servlet Config for two reasons:
The
See the javadoc for the
Additional methodsAdditional methods provided:
ServletContextWrapper
This is not an implicit object per see (as it is not accessible as
an instance variable). It is available by calling
However, Cactus wraps the
See the javadoc for the
Additional methodsAdditional methods provided:
session
Cactus does not wrap the response.
By default, Cactus always creates an HTTP session for your test
case. It is possible to tell it not to do so by calling the
Tips and TricksParameter initialisation
If your code under test make use of any of the servlet methods
inherited from 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. SampleThis is a very basic sample intended to give you a flavour of Servlet unit testing. Check the distribution samples for extensive examples.
This example is for Cactus 1.2 and above as it uses the new
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()); } |