# ----------------------------------------------------------------------------- # howto_testcase.txt # # $Id: howto_testcase.txt,v 1.2 2004/01/26 07:30:48 miyachi Exp $ # ----------------------------------------------------------------------------- * HowTo for write YOUR Enhydra Application TestCase. +-----------------------------------------------------------------------+ | You SHOULD read http://jakarta.apache.org/cactus/howto_testcase.html, | |before reading this document. | +-----------------------------------------------------------------------+ * Step 1: Imports You need to include the following imports in your test class (junit.framework.* is needed because Cactus uses JUnit as the client side application for calling the tests): ---------------------------------------------------------------------------------------- import org.apache.cactus.*; import junit.framework.*; import net.sourceforge.cactus4enhydra.EnhydraAppTestCase; ---------------------------------------------------------------------------------------- * Step 2: Extend a Cactus TestCase class or reuse a JUnit TestCase * Option A: Extend a Cactus TestCase class We need to create a class (our test class) that extends one of Cactus test cases, depending on what we are testing: ---------------------------------------------------------------------------------------- public class TestSamplePresentation extends EnhydraAppTestCase { ... } ---------------------------------------------------------------------------------------- * Option B: reuse a JUnit TestCase * Step 3: Standard JUnit methods As in a normal JUnit test case, define the following standard JUnit methods : * A constructor with a single parameter (it is the test name). * (optional): A main() method in which you start a JUnit test runner if you want your test to be executable, * (optional): A suite() method to list the tests that should be executed by your test class (default is to include all method starting with "test"). ---------------------------------------------------------------------------------------- public TestSamplePresentation(String theName) { super(theName); } public static void main(String[] theArgs) { junit.swingui.TestRunner.main(new String[] {TestSampleServlet.class.getName()}); } public static Test suite() { return new TestSuite(TestSampleEnhydraApp.class); } ---------------------------------------------------------------------------------------- * Step 4 (optional): setUp() and tearDown() methods * Step 5 (optional): begin() and end() methods * Step 6: testXXX() methods ---------------------------------------------------------------------------------------- public testXXX() { ... } public void testReadEnhydraAppOutputStream() throws HttpPresentationException, IOException { SamplePresentation sample= new SamplePresentation(); sample.run(comms); } public void testGetMethod() throws HttpPresentationException, IOException { SamplePresentation sample = new SamplePresentation(); sample.run(comms); assertEquals(comms.request.getMethod(), "GET"); assertEquals(comms.request.getParameter("param"), "value"); } ---------------------------------------------------------------------------------------- * Step 7 (optional): beginXXX() methods ---------------------------------------------------------------------------------------- public void beginXXX(WebRequest theRequest) { ... } public void beginGetMethod(WebRequest theRequest) { theRequest.addParameter("param", "value"); } ---------------------------------------------------------------------------------------- * Step 8 (optional): endXXX() methods ---------------------------------------------------------------------------------------- public void endXXX(WebResponse theResponse) { ... } public void endReadEnhydraAppOutputStream(WebResponse theResponse) { String result = theResponse.getText(); assertEquals(result, "<html><head></head><body>Enhydra Application</body></html>"); } ---------------------------------------------------------------------------------------- *Provided Implicit Objects Cactus(and Cactus4Enhydra) automatically initializes the implicit objects for you and they are made available to your setUp(), testXXX() and tearDown() methods as instance variables of the EnhydraAppTestCase class (and thus as instance variables of your test case class as it extends EnhydraAppTestCase). The provided implicit objects are: comms Instance variable name: comms Class name : com.lutris.appserver.server.httpPresentationComms Such as application, request, response, session, sessionData, xmlcFactory are reference from comms.