Document directory
- I. Working Principle
- Ii. Configuration
- 3. How to Write test cases (testcase)
Original address: http://www.javaresearch.org/article/showarticle.jsp? Column = 526 & Thread = 11534
Cactus is a simple and easy-to-use server-side testing framework that allows developers to easily test server-side programs. Cactus is an extension of JUnit, but it is somewhat different from JUnit. Cactus testing is divided into three different test categories: jsptestcase, servlettestcase, and filtertestcase, rather than a testcase like JUnit. The cactus test code consists of two parts: the server side and the client side. They work together. What are the advantages of cactus over JUnit in testing server applications?
Generally, EJB, Servlet, and JSP are run on the server. If you use JUnit for testing, your test is performed on the client, this makes the running environment and test environment in different system environments, which sometimes lead to different test results.
The Local interface in ejb2.0 cannot be called remotely. It is hard to test with JUnit, while cactus's redirector is on the server side and can run in a container with EJB, which allows it to directly access the local interface.
In an EJB application, some front-end applications generally access ejbs, such as JSP, Servlet, and JavaBean. This means that you need a testing framework to test these front-end components. Cactus provides testing methods for all these components.
The combination of cactus and ant can easily complete automated testing, reducing a lot of work.
I. Working Principle
1. JUnit test runner calls yyytestcase. runtest (). This method is used to find beginxxx (servlettestrequest)
2. yyytestcase. runtest () opens an HTTP connection to redirector proxy
3. redirector Proxy:
Create an instance of test class
Create cactus wrapper for some server objects (httpservletrequest, servletconfig, and servletcontext)
If necessary, create an http session
4. redirector proxy uses reflection to execute setup (), testxxx (), and teardown () of the test class ()
5. testxxx () calls the server side classes method and verifies the test result through JUnit's assert API.
6. If the test fails, the testxxx () method throws an exception and the redirector proxy captures the exception.
7. The redirector proxy returns the exception-related information to the client, which is printed by JUnit.
8. If no exception occurs, yyytestcase. runtest () searches for and executes endxxx (httpurlconnection). Here you can use JUnit asserts to check the returned HTTP header and Servlet output stream
Servlet redirector proxy
The client opens two HTTP connections, one for testing and retrieving the servlet output stream; the other for retrieving the test results. The test results are stored in a variable and placed in servletcontext for retrieval by the second connection.
JSP redirector proxy
The client opens two connections. One is used to connect to the JSP redirector and execute test to retrieve the JSP output stream. The other is used to connect the servlet redirector to retrieve the test result. The test results are also stored in a variable and placed in servletcontext for retrieval by the second connection.
Ii. Configuration
Client
Cactus. properties: Configure the server Steering Gear address
Cactus. servletredirectorurl = http: // localhost: 8080/test/servletredirector/(Note: The end is "/")
Cactus. jspredirectorurl = http: // localhost: 8080/test/jspredirector/
Cactus. filterredirectorurl = http: // localhost: 8080/test/filterredirector/
Log_client.properties configure log4j
Cactus. jar contains a saved log_client.properties and log_server.properties file. If you want to provide your own log_client.properties file, you must put this file in classpath and before cactus. jar.
Server
Web. xml
To work with Cactus. properties on the client, deploy an application named test on the server and write a configuration file web. XML, as shown in the following example:
<Web-app>
<Filter>
<Filter-Name> filterredirector </filter-Name>
<Filter-class> org. Apache. Cactus. server. filterredirector </filter-class>
</Filter>
<Filter-mapping>
<Filter-Name> filterredirector </filter-Name>
<URL-pattern>/filterrecirector/</url-pattern>
</Filter-mapping>
... The classes and URLs of servletredirector and jspredireare also configured. The special feature is that jspredireclass configuration is as follows:
<Servlet>
<Servlet-Name> jspredirector </servlet-Name>
<JSP-File>... (/somedir)/jspredirector. jsp </JSP-File>
<Init-param>
<Param-Name>... </param-Name>
<Param-value>... </param-value>
</Init-param>
</Servlet>
Note: If you use jspredirector (inherited from jsptestcase), you must copy jspredirector. jsp to a directory (somedir in Web. XML ). The jspredirector. jsp file is in the sample/web/test file and serves as the proxy to call the server-side unit tests. The file example is as follows:
- <% @ PageImport= "Org. Apache. Cactus. server. *" session = "false" %> <%
- Jspimplicitobjects objects =NewJspimplicitobjects ();
- Objects. sethttpservletrequest (request );
- Objects. sethttpservletresponse (response );
- Objects. setservletconfig (config );
- Objects. setservletcontext (application );
- Objects. setjspwriter (out );
- Objects. setpagecontext (pagecontext );
- Jsptestredirector redire=NewJsptestredire ();
- Redirector. doget (objects );
- %>
Log_server.properties
In cactus. jar, if you want to customize it, you 'd better package the modified log_server.properties to cactus. jar to overwrite the original file.
3. How to Write test cases (testcase)
To write a test case:
1. Import
- ImportOrg. Apache. cactsu .*;
- ImportJUnit. 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 the name of test
2) Main (string []) // start a JUnit test runner
JUnit. UI. testrunner. Main (New String [] {testsampleservlet. Class. getname ()});
3) Suite () // static method. The return value is test.
Return new testsuite (testsampleservlet. Class)
4. Setup (), teardown ()
5. testxxx ()
One instance:
Public void testxxx (){
Sampleservlet servlet = new sampleservlet ();
Session. setattribute ("name", "value ");
Servlet. dosomething (request );
Assertequals ("something", result );
Assertequals ("othervalue", session. getattribute ("othername "));
}
6. beginxxx (webrequest) // optional
Set all http-related parameters in webrequest, mainly including:
Setmethod (string)
Setautomaticsession (Boolean)
Seturl (...)
Addparameter (string, string.
Then, you can call getquerystring () and getheader () of httpservletrequest in the testxxx () method.
Note: This method is executed on the client first with the testxxx () method. Therefore, do not attempt to access any of the class variables that represent API objects (their values are null ). The same is true for endxxx.
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 two purposes:
4) provides additional methods. httpconfigwrapper has two more methods than javax. servlet. servletconfig.
Setservletname (string)
Setinitparameter (string, string) // same effect as setting in Web. xml
5) The encapsulated servletcontext can be returned,
Session? C javax. servlet. http. httpsession
Tip 1: If you use any method that inherits from javax. servlet. for methods of genericservlet (such as log () and getservletconfig (), you need to call the servlet Init (servletconfig) method to instance the servletconfig object. Example:
Public void testxxx (){
Myservlettotest servlet = new myservlettotest ();
Servlet. INIT (config );
... Servlet. somemethodtotest ();
}
For jsptestcase, 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 two purposes:
6) provide additional methods. httpconfigwrapper has two more methods than javax. servlet. servletconfig.
Setservletname (string)
Setinitparameter (string, string) // same effect as setting in Web. xml
7) The encapsulated servletcontext can be returned,
Session? C javax. servlet. http. httpsession
Tip 1: If you use any method that inherits from javax. servlet. for methods of genericservlet (such as log () and getservletconfig (), you need to call the servlet Init (servletconfig) method to instance the servletconfig object. Example:
Public void testxxx (){
Myservlettotest servlet = new myservlettotest ();
Servlet. INIT (config );
... Servlet. somemethodtotest ();
}