Let's take a look at the example of how cactus and jetty are integrated for integration testing in JUnit in action.Source codeIt is very simple, but it is always unable to run. It took several hours to get it done. Cactus I use is 1.8.1.Code, There are two classes:
Servlet to be tested:
Package junitbook. container; </P> <p> Import javax. servlet. HTTP. httpservlet; <br/> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpsession; </P> <p> public class sampleservlet extends httpservlet <br/> {<br/> Public Boolean isauthenticated (httpservletrequest request) <br/>{< br/> httpsession session = request. getsession (false); </P> <p> If (session = NULL) <br/>{< br/> return false; <br/>}</P> <p> string authenticationattribute = <br/> (string) session. getattribute ("authenticated"); </P> <p> return Boolean. valueof (<br/> authenticationattribute ). booleanvalue (); <br/>}< br/>
Test class:
Package junitbook. container; </P> <p> Import Org. apache. cactus. servlettestcase; <br/> Import Org. apache. cactus. webrequest; </P> <p> public class testsampleservletintegration extends servlettestcase <br/>{< br/> private sampleservlet; </P> <p> Public Static Test Suite () <br/>{< br/> system. setproperty ("cactus. contexturl "," http: // localhost: 8080/test "); // this step is very important, be sure to have </P> <p> testsuite suite = new testsuite ("all tests with Jetty"); <br/> suite. addtestsuite (testsampleservletintegration. class); <br/> return New jetty5xtestsetup (suite); <br/>}</P> <p> protected void setup () <br/>{< br/> servlet = new sampleservlet (); <br/>}</P> <p> Public void testisauthenticatedauthenticated () <br/> {<br/> session. setattribute ("authenticated", "true"); </P> <p> asserttrue (servlet. isauthenticated (request); <br/>}</P> <p> Public void testisauthenticatednotauthenticated () <br/>{< br/> assertfalse (servlet. isauthenticated (request); <br/>}</P> <p> Public void beginisauthenticatednosession (webrequest request) <br/>{< br/> request. setautomaticsession (false); <br/>}</P> <p> Public void testisauthenticatednosession () <br/>{< br/> assertfalse (servlet. isauthenticated (request); <br/>}< br/>
There are several notes:
1. The test class should inherit from servlettestcase, with built-in session, request, and response, which can be directly used
2.Jetty cannot be downloaded separately.Each cactus release has a built-in jetty, which can be found in the lib directory. It is obviously rewritten. If you download it separately, it will definitely throwJava. Lang. nosuchmethodexception: org. mortbay. Jetty. NiO. selectchannelconnector. setport (Java. Lang. String ),In a separate jetty release, the setport parameter is int, so an exception is thrown. I don't know why cactus does this.
3. dependent on the jar of commons Codec
4. If Jetty is 5.x, jetty5xtestsetup is used. If Jetty is 6.x, jetty6xtestsetup is used. But the current cactus is still using jetty5.1.9 (Org. mortbay. jetty-5.1.9.jar), so jetty5xtestsetup is used.
In this case, Jetty started very quickly. In a short time, we need to study how to test struts2.