Original address:
http://www.javaresearch.org/Article/showArticle.jsp?column=526&thread=11534
Cactus is a simple, easy-to-use server-side test framework that allows developers to test the server-side programs. Cactus is an extension of Junit, but it is different from JUnit. CACTUS test is divided into three different test categories, JSPTestCase, ServletTestcase, FilterTestCase, not like Junit, a TestCase. CACTUS's test code has two parts of the server side and the client, and they work together. What is the advantage over the test server-side application?
General EJB or Servlet, JSP is running on the server. If you use JUnit test, your test is on the client, which makes the running environment and test environment in different system environments, this sometimes different tests result.
Local Interface in EJB2.0 does not allow remote calls. With JUnit is not tested, and CACTUS's Redirector is located on the server, and can run in a container with the EJB, which allows it to access local interface directly.
In an EJB application, there are some front-end applications to access EJBs, such as: JSP, Servlet, JavaBean. This means that you need a test framework to test the components of these front ends. CACTUS provides test methods for all of these components.
Cactus and Ant are well combined, which can easily complete the automation test, which reduces a lot of workload.
I. Working principle
1. JUnit Test Runner calls YYYTESTCASE.RUNTEST (), this method is looking for beginxxx (servlettestRequest)
2. YYYTESTCASE.RUNTEST () Opens a HTTP connection to Redirector Proxy
3. Redirector Proxy makes the following
Creating an instance of TEST CLASS
Create some Server objects (HTTPSERVLETREQUEST, ServletConfig, servletContext) CACTUS WRAPPER
If necessary, create an HTTP session
4. Redirector Proxy Via Reflection, executes setup (), testxxx (), TestXxx (), TESTXXX ()
5. Testxxx () Call the method of Server Side Classes, and verify the test results through the JUnit ASSERT API
6. If the test fails, the testXXX () method throws an exception, Redirector Proxy will capture exceptions.
7. Redirector proxy returns the exceptional information to the client, and JUnit prints this information.
8. If there is no exception, YYYTESTCASE.RUNTEST () finds and executes Endxxx (HttpurlConnection), where you can check back HTTP header, Servlet Output Stream
Servlet Redirector Proxy
The client opens 2 HTTP connections, one for performing testing and retrieving the servlet output flow; another retrieval test result. The test results are stored in a variable and are placed to servletContext for the second connection to return.
JSP Redirector Proxy
The client opens 2 connections, one for connecting the JSP Redirector to perform TEST, retrieve the JSP output stream; the second connection servlet redirector retrieves the test result. The test results are also stored in one variable and are placed to servletContext for the second connection. Second, the configuration
Client
Cactus.properties Configure the address of the Server-side steering
Cactus.ServletRedirectorURL =
http: // localhost: 8080 / test / servletredirector / (Note: End is "/")
CACTUS.JSPREDIRECTORURL =
http: // localhost: 8080 / test / jspredirector /
Cactus.filterRedirectorURL =
Http: // localhost: 8080 / test / filterRedirector /
Log_client.properties Configure log4j
In CACTUS.jar already contains an independent log_client.properties file, Log_Server.properties file. If you want to provide your own log_client.properties file, you should put this file in the classpath and must be placed before cactus.jar.
SERVER
Web.xml
In order to cooperate with the CACTUS.Properties above the Client side, you need to deploy a application named Test in the Server side, and write a configuration file Web.xml, as follows:
filter>
filter-mapping>
... Also configure the class and URL of ServletRedirector, JSPredirector, and the specials of JSPRedirector are:
init-param>
servlet>
Note: If you use JspRedirector (ie inherited JSPTestCase), you must copy JSPRedirector.jsp to a directory (Somedir in web.xml). JSPRedirector.jsp files in Sample / Web / Test, as the Unit Tests of the Server end as Proxy, as follows:
<% @ Page import = ". Org.apache.cactus.server *" session = "false"%> <% JspImplicitObjects objects = new JspImplicitObjects (); objects.setHttpServletRequest (request); objects.setHttpServletResponse (response); objects. setServletConfig (config); objects.setServletContext (application); objects.setJspWriter (out); objects.setPageContext (pageContext); JspTestRedirector redirector = new JspTestRedirector (); redirector.doGet (objects);%> log_server.properties
In CACTUS.jar, if you want to customize, you'd better pack the modified log_server.properties to CACTUS.JAR, overwrite the original file.
Third, how to write test cases (Testcase)
Steps to write a Test Case:
IMPORT
Importsu. *; import junit.framework. *;
2. Extends a Cactus Testcase
Public Class TestsampleServlet Extends Servlet / JSP / FilterTestCase {
3. Standard JUnit method
1) Constructor
TestsampleServlet (String thename) {
Super (thename);
} // thename is Test's Name
2) Main (String []) // Start a JUnit Test Runner
JUnit.ui.teStrunner.main (new string "{testsampleServlet.class.getname ()});
3) Suite () // Static method, return value is TEST
Return New Testsuite (TestsampleServlet.class)
4. Setup (), Teardown ()
5. Testxxx ()
An example:
Public void testxxx () {
SampleServlet servlet = new SampleServlet ();
Session.setttribute ("Name", "Value");
servlet.dosomething (request);
askERTEQUALS ("Something", Result;
Assertequals ("OtherValue", session.getattribute ("OtherName"));
}
6. Beginxxx (WebRequest) // Optional
Set all HTTP related parameters in WebRequest, mainly:
SetMethod (String)
SetAutomaticSession (Boolean)
Seturl (...)
AddParameter (String, String), etc.
Then, you can call the entQueryString (), getHeader (), etc. of HttpServletRing (), getHeader (), etc. in the Testxxx () method. Note: This method is executed in the client side, first with the testXXX () method; so don't try to access any The Class Variables That Repesent API Objects (Their Values Are Null). Endxxx () is also the same.
Endxxx (httpurlconnection) // optional
For servlettestcase, the following implicit objects are provided:
Request? C org.apache.cactus.server.httpservletRequestWrapper
Response? C javax.servlet.http.httpservletResponse
config? C org.apache.cactus.server.ServletConfigWRAPPER
Packaging config has 2 purposes:
4) Provide additional methods, HTTPConfigWrapper is more than javax.servlet.ServletConfig more than 2 methods
SetServletName (String)
SetInitParameter (String, String) / / is equipped with the same effect in web.xml
5) You can return to the packaged servletContext,
Session? C javax.servlet.http.httpsession
Tip 1: If you use any way to inherit the Javax.Servlet.GenericServlet (such as: log (), getServletConfig (), then you need to call the servlet's init (servletConfig) method to instance ServletConfig objects. For example, as follows:
Public void testxxx () {
MyServlettotest servlet = new myservlettotest ();
servlet.init (config);
... servlet.somemethodtotest ();
}
For JSPTestCase, the following implied objects are provided:
Request? C org.apache.cactus.server.httpservletRequestWrapper
Response? C javax.servlet.http.httpservletResponse
config? C org.apache.cactus.server.ServletConfigWRAPPER
Packaging config has 2 purposes:
6) Provide additional methods, HTTPConfigWrapper is more than javax.servlet.ServletConfig more than 2 methods
SetServletName (String)
SetInitParameter (String, String) / / is equipped with the same effect in web.xml
7) You can return to the packaged servletContext,
Session? C javax.servlet.http.httpsession
Tip 1: If you use any way to inherit the Javax.Servlet.GenericServlet (such as: log (), getServletConfig (), then you need to call the servlet's init (servletConfig) method to instance ServletConfig objects. For example, as follows:
Public void testxxx () {myservlettotest servlet = new myservlettotest ();
servlet.init (config);
... servlet.somemethodtotest ();
}