![]() | ![]() |
Apache > Jakarta > Cactus > Writing Tests | Docs for: v1.7.2 | v1.7 Last update: August 30 2007 |
IntroductionThis tutorial explains how to write a test case, using Cactus. There are several types of test cases: test cases for writing servlet unit tests, test cases for writing taglib unit tests and test cases for writing filter unit tests. We will first cover the principles which are generic to all test cases and then we will dive into the details specific to each test case.
In order to help writing test case we highly suggest to have a look
at the examples provided as part of the Cactus distribution. Also,
all the best practices for JUnit also applies to Cactus test cases
as they are in essence JUnit test cases.
General principlesTo write a test case, please follow the steps defined below. Step 1: Imports
You need to include the following imports in your test class (
import org.apache.cactus.*; import junit.framework.*; Step 2: Extend a Cactus TestCase class or reuse a JUnit TestCaseOption A: Extend a Cactus TestCase classWe need to create a class (our test class) that extends one of Cactus test cases, depending on what we are testing:
Option B: reuse a JUnit TestCase
Cactus is able to run pure JUnit TestCase on the server side.
This is done by using the public class TestJUnitTestCaseWrapper extends TestCase { public static Test suite() { ServletTestSuite suite = new ServletTestSuite(); suite.addTestSuite(TestJUnitTestCaseWrapper.class); return suite; } public void testXXX() { } } Step 3 (optional): setUp() and tearDown() methods
As in JUnit, you can define a
As in JUnit, the Step 4 (optional): begin() and end() methods
The Step 5: testXXX() methods
As in JUnit, the main method for a test is the
In your
For example: public void testXXX() { // Initialize class to test SampleServlet servlet = new SampleServlet(); // Set a variable in session as the doSomething() method that we are testing need // this variable to be present in the session (for example) session.setAttribute("name", "value"); // Call the method to test, passing an HttpServletRequest object (for example) String result = servlet.doSomething(request); // Perform verification that test was successful assertEquals("something", result); assertEquals("otherValue", session.getAttribute("otherName")); } Step 6 (optional): beginXXX() methods
For each XXX test case, you can define a corresponding
The signature of the begin method is: public void beginXXX(WebRequest theRequest) { [...] }
where
The full description of all the HTTP related parameters that you can
set can be found in the javadoc for the
The Step 7 (optional): endXXX() methods
For each XXX test case, you can define a corresponding
For versions of Cactus up to v1.1, the signature of the end method is: public void endXXX(HttpURLConnection theConnection) { [...] }
... and some helper methods to extract the response content and
cookies were provided in the However, beginning with Cactus 1.2, this signature has been deprecated. There are now 2 possible signatures for the end method, depending on whether you need to perform sophisticated checks on the content of what is returned or not. For complex checking, we have integrated with the HttpUnit framework. See the HttpUnit tutorial for the end method signatures and a full description.
The TestCase specific detailsBefore reading any of the following detailed tutorials, make sure you have read the previous general principles. |