Cactus instance description

Source: Internet
Author: User

About cactus

. Overview

Cactus enables seamless expansion of the JUnit testing framework to conveniently test server applications.Program. Cactus can be used in the following scenarios:

    • Test the servlet and any use such as httpservletrequest, httpservletresponse ,...... Such an object'sCode. Use servlettestcase.
    • Test the filter and any operations such as filterconfig ,...... Code of such an object. Use filtertestcase.
    • Test JSP. Use servlettestcase or jsptestcase.
    • Test taglibs and any use of pagecontext ,...... Code of such an object. Use jsptestcase.
    • Test EJB. Servlettestcase, jsptestcase, or filtertestcase.

Cactus is also very easy to use. The test classes you write only need to inherit from servlettestcase, jsptestcase, and filtertestcase (both inherit from the testcase of JUnit ). After writing the test code, you need to start the Web Container and then execute the test code. In the following sections, we will explain in detail through examples.

A subproject of the cactus project Apache Jakarta commons. The URL is http://jakarta.apache.org/commons/cactus /.

. Testcase framework

In cactus, The testcase we write is different from that in JUnit. Let's take a look at the code, as shown below:

Public class testsample extendsservlettestcase/jsptestcase/filtertestcase {
Public testsample (string testname ){
Super (testname );
}
Public void setup (){
}
Public void teardown (){
}
Public void beginxxx (webrequest therequest ){
}
Public void testxxx (){
}
Public void endxxx (webresponse theresponse ){
}

The above is a complete code framework of the cactus test class. The extends part must inherit different classes based on the different goals you test (described in the Introduction ).

In addition, we noticed two new methods, beginxxx and endxxx, which are executed before and after testxxx, respectively, unlike setup and teardown, beginxxx and endxxx are executed before testxxx, while Setup and teardown are executed before each testxxx method. In addition, beginxxx and endxxx are client code. Therefore, server objects such as request cannot be used in these two methods.

Note that before cactus V1.1 (including V1.1), The endxxx method is in the form of public void endxxx (httpurlconnection THECONNECTION ), in cactus v1.2, there are two possible forms:

    • Public void endxxx (Org. Apache. Cactus. webresponse theresponse );
    • Public void endxxx (COM. meterware. httpunit. webresponse theresponse );

The difference is that the referenced package is different. Why? Because cactus has integrated the httpunit component in v1.2. If you are familiar with the httpunit component, I want to understand why to integrate httpunit. Let's take a look at a piece of code to compare the differences between the two:

Public void endxxx (Org. Apache. Cactus. webresponse theresponse ){

String content = theresponse. gettext ();

Assertequals (content, "<HTML> <body>

}

Public void endxxx (COM. meterware. httpunit. webresponse theresponse ){

Webtable table = theresponse. gettables () [0];

Assertequals ("rows", 4, table. getrowcount ());

Assertequals ("columns", 3, Table. getcolumncount ());

Assertequals ("Links", 1, Table. gettablecell (0, 2). getlinks (). Length );

}

Of course, in actual application, you need to select different endxxx based on different needs. For the difference between the two webresponse, see the API Doc of the two.

How to Write a test in cactus

. Write test code

First, we provide the code of the tested class, which is a servlet:

Public class sampleservlet extends httpservlet {

Public void doget (httpservletrequest therequest,

Httpservletresponse theresponse) throws ioexception {

Printwriter PW = theresponse. getwriter ();

Theresponse. setcontenttype ("text/html ");

PW. Print ("<HTML>

PW. Print ("a GET request ");

PW. Print ("</body>

}

Public String checkmethod (httpservletrequest therequest ){

Return therequest. getmethod ();

}

}

The test framework in cactus is provided above. The example below is a part of the instance that comes with cactus, as shown below:

Public class testsampleservlet extends servlettestcase {

Public void testreadservletoutputstream () throws ioexception {

Sampleservlet servlet = new sampleservlet ();

Servlet. doget (request, response );

}

Public void endreadservletoutputstream (webresponse theresponse)

Throws ioexception {

String expected = "<HTML>

String result = theresponse. gettext ();

Assertequals (expected, result );

}

Public void beginpostmethod (webrequest therequest ){

Therequest. addparameter ("Param", "value", webrequest. post_method );

}

Public void testpostmethod (){

Sampleservlet servlet = new sampleservlet ();

Assertequals ("Post", Servlet. checkmethod (request ));

Assertequals ("value", request. getparameter ("Param "));

}

}

The first method, testreadservletoutputstream, calls doget, is equivalent to submitting a request on the client, and then generates a feedback after the servlet processes it. Therefore, in the endreadservletoutputstream method, we call the response method to determine whether the feedback meets the expected results.

The second method is testpostmethod. Before that, there is a beginpostmethod. In this method, we add a form data Param to the request using the POST method, and the value is "value ". In testpostmethod, we need to verify whether the form data is submitted to the server servlet in post mode. Therefore, we have seen two assertequals and made judgments respectively. Here we need to note the difference between the request in the therinpostmethod method and the request in testpostmethod. As we mentioned earlier, beginpostmethod is executed on the client, therefore, all the operations in the method are actually simulated page operations. For example, the above table data is set, while testpostmethod is executed by the server, and the request is also performed by the server.

Configure cactus. properties and web. xml

Cactus. Properties

    • Cactus. contexturl

This attribute is required and specifies the web application access address.

Example: Cactus. contexturl = http: // localhost: 8080/test

    • Cactus. servletredirectorname

Optional. The ing name of cactus servlet redirector is specified when the test class inherits servlettestcase. Default: servletredirector

Example: Cactus. servletredirectorname = servletredirector

    • Cactus. jspredirectorname

Optional. The ing name of cactus JSP redirector is specified when the test class inherits servlettestcase. Default: servletredirector

Example: Cactus. jspredirectorname = jspredire

    • Cactus. filterredirectorname (for J2EE API 1.3 only)

Optional. The ing name of cactus filter redirector is specified when the test class inherits servlettestcase. Default: servletredirector

Example: Cactus. filterredirectorname = filterredirector

Cactus. properties you can place under WEB-INF/classes.

Web. xml

In web. XML, you must specify the corresponding cactus redirector for the corresponding test class.

Servlettestcase corresponds to org. Apache. Cactus. server. servlettestredirector

Jsptestcase/jspredirector. jsp

Filtertestcase corresponds to org. Apache. Cactus. server. filtertestredirector

<Web-app>

<Filter>

<Filter-Name> filterredirector </filter-Name>

<Filter-class> org. Apache. Cactus. server. filtertestredirector </filter-class>

</Filter>

<Filter-mapping>

<Filter-Name> filterredirector </filter-Name>

<URL-pattern>/filterredirector </url-pattern>

</Filter-mapping>

<Servlet>

<Servlet-Name> servletredirector </servlet-Name>

<Servlet-class> org. Apache. Cactus. server. servlettestredirector </servlet-class>

</Servlet>

<Servlet>

<Servlet-Name> jspredirector </servlet-Name>

<JSP-File>/jspredirector. jsp </JSP-File>

</Servlet>

<Servlet-mapping>

<Servlet-Name> servletredirector </servlet-Name>

<URL-pattern>/servletredirector </url-pattern>

</Servlet-mapping>

<Servlet-mapping>

<Servlet-Name> jspredirector </servlet-Name>

<URL-pattern>/jspredirector </url-pattern>

</Servlet-mapping>

</Web-app>

If your test class inherits jsptestcase, You need to place the jspredirector. jsp file in the path you specified in Web. xml.

Installation instructions

    • When cactus is used, strutstest. Jar also requires support for the following packages. Packages can be placed under WEB-INF/lib

As follows:

      • Client, the following package is required

JUnit. Jar

Servlet. Jar

Cactus. Jar

Httpclient. Jar

Commons-logging.jar

Httpunit. jar, tidy. jar, and xerces. Jar (optional. If you have integrated httpunit, you need to use httpunit in endxxx)

      • The server (Web Container) needs the following package

Cactus. Jar

JUnit. Jar

Aspectjrt. Jar

Commons-logging.jar

    • write the test code and place the class under the WEB-INF/classes
    • the tested code is also placed in the WEB-INF/classes
    • write the cactus. properties and web. xml configuration files.
    • Start the Web Container
    • run the test code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.