Plant Cactus in NetBeans IDE 4.1

Cactus is the current cool thing, everywhere you go you hear about it. Well, anyway, everywhere I go, which doesn't say all that much since I only leave the building to teach English. Nonetheless, according to a JavaOne presentation I've been looking through, it's "a simple test framework for unit testing server-side Java code (Servlets, EJBS, Tag Libs, Filters,...)". I've been working through the process of setting Cactus up in NetBeans, and using it over Tomcat, so that I can find out what all the buzz is about.

So, as a quick preview, here's what I ended up with at the end of the procedure:

And here's a brief run-down of the procedure for testing servlets (it assumes you've already got a web application):

  1. Get the Cactus software. Download it here. Then, once you've unpacked and unzipped everything, do the following to set everything up:

    • Make Cactus JARs available to Tomcat. Copy the Cactus JARs into Tomcat's common/lib folder. (Don't worry about servletapi.jar though.)

    • Make Cactus JARs available to your IDE project. Add the JARs you copied to Tomcat's common/lib to your application's Compile Test Classpath. (Right-click the project, choose Properties, click Libraries, and add them to the Compile Tests tab).

    • Make the Cactus report formatter available to your IDE project. Go to http://jakarta.apache.org/cactus/misc/ and download cactus-reports.xsl. Put it in your application's test folder.
  2. Modify web.xml. Add servlet definitions and mappings in web.xml for ServletRedirector and ServletTestRunner:

    <servlet> 
      <servlet-name>ServletRedirector</servlet-name> 
      <servlet-class> 
         org.apache.cactus.server.ServletTestRedirector
      </servlet-class> 
      <init-param> 
       <param-name>param1</param-name> 
       <param-value>value1 used for testing</param-value> 
      </init-param> 
    </servlet> 
    
    <servlet> 
      <servlet-name>ServletTestRunner</servlet-name> 
      <servlet-class> 
        org.apache.cactus.server.runner.ServletTestRunner
      </servlet-class> 
    </servlet> 
    
    <servlet-mapping> 
      <servlet-name>ServletRedirector</servlet-name> 
      <url-pattern>/ServletRedirector</url-pattern> 
    </servlet-mapping> 
    
    <servlet-mapping> 
      <servlet-name>ServletTestRunner</servlet-name> 
      <url-pattern>/ServletTestRunner</url-pattern> 
    </servlet-mapping>

  3. Copy or create Cactus test classes. Copy classes and their related test classes from the Cactus distribution to your own application's folders. For example, get SampleServlet.java from samples\\servlet\\src\\java\\org\\apache\\cactus\\sample\\servlet and put it in a package in your Source Packages and then copy TestSampleServlet.java from samples\\servlet\\src\\test-cactus\\org\\apache\\cactus\\sample\\servlet to a package in your application's Test Packages folder.

    Alternatively, assuming you know how to do it, just create your own Cactus test classes.

  4. Modify your Ant build file. Now you'll need to add the following to your build.xml Ant script:

    <target name="-post-compile-test" depends="run">
       <copy todir="${build.web.dir.real}"
              file="${test.src.dir}/org/me/hello/cactus-report.xsl" />
       <copy todir="${build.classes.dir.real}">
          <fileset dir="${build.test.classes.dir}" excludes="\*\*/\*.java"/>
       </copy>
       <input message="Which file do you want to run?"
              validargs="TestSampleServlet,TestSampleServletConfig" 
            addproperty="testfile"/> 
       <antcall target="run-cactus-test" />
    </target>

    <target name="run-cactus-test" depends="run-deploy,run-display-test"/>

    <target name="run-display-test" depends="run-deploy">
       <nbbrowse url="http://localhost:8084/CactusTest/ServletTestRunner?suite=org.me.hello.${testfile}&xsl=cactus-report.xsl"/>
    </target>

Now, when you select the project's node in the Projects window, and then choose Run > Test, you're prompted to select the class that you'd like to run. When you do this, the testfile property is set, specifying the test class that Cactus will run. And then a nice report is produced, using the cactus-report.xsl file, which gives it a friendly structure (click to enlarge):

Comments:

Hi I am very thank full to you I am looking for the issue like I am working in a problem where I need to generate the html test report from the cactus using junit in wsad(web sphere application devloper). please correct me if I am wrong. and Regarding the contents of build.xml you had specified the part of it please send the total code of build.xml. please update me as soon an possible fast response is great for me

Posted by laxman on July 21, 2005 at 01:50 AM PDT #

Hi Laxman,

I'd be very happy to help, but I don't know exactly what you need! You would like me to send you the whole build.xml file? I can do that, if that's what you want. Please note that the build.xml file is the standard build.xml file that NetBeans creates for you when you create a project -- the only difference is the additional targets I specified in step 4 above.

Posted by Geertjan on July 21, 2005 at 01:56 AM PDT #

As I told you I wanted to generate the test report in cactus. can I use above mentioned information. If I can,the contents in build.xml is partly written. how do I do the build.xml.I am not sure how to place the above code in build.xml. please guide me regarding that. Thanks and Regards laxman

Posted by laxman on July 21, 2005 at 02:23 PM PDT #

Hi I am very thank full to you I am looking for the issue like I am working in a problem where I need to generate the html test report from the cactus using junit in wsad(web sphere application devloper). please correct me if I am wrong. and Regarding the contents of build.xml you had specified the part of it please send the total code of build.xml. please update me as soon an possible fast response is great for me

Posted by laxman on July 21, 2005 at 02:55 PM PDT #

Hi, I followed your guide and it gives the following errors before I can build it. ../CactusTest\\build.xml:80: The reference to entity "xsl" must end with the ';' delimiter. Ok so I removed it which is probably wrong and I then get this error Wrapper cannot find servlet class org.apache.cactus.server.runner.ServletTestRunner What am I doing wrong here? Thanks for any advice

Posted by pmoc on July 22, 2005 at 11:13 PM PDT #

Hi Pmoc, the reason for your error is that the <tt>run-display-test</tt> target in step 4 above is wrong. In step 4, the final part of the <tt>nbbrowse</tt> goes like this:

<tt>&xsl=cactus-report.xsl</tt>

However, instead of "&" you should have this:

& a m p ;

Except, please remove the spaces between the characters. (The reason that it is not displayed correctly in step 4 above is that if there are no spaces between "a m p ;", it is not displayed. I hope this is clear -- if not, please leave another message!

Posted by Geertjan on July 22, 2005 at 11:40 PM PDT #

Hi Laxman, if you use NetBeans, the <tt>build.xml</tt> file is created for you. You don't need to create it yourself. All you need to do is add the Ant targets specified in step 4 above. I don't know anything about web sphere application developer, I don't know how <tt>build.xml</tt> works with that IDE. I can send you my <tt>build.xml</tt> if you want, I'm sure you could integrate it into web sphere application developer somehow. If you would like me to do this, please leave another message and say so!

Posted by Geertjan on July 22, 2005 at 11:48 PM PDT #

please send me the complete build.xml file I did some modifications on the build file and mapped according to my file. I copied the "cactus-report.xsl" into my workspace with out any modifications Is there any modifications required in that file. and I did the changes in web.xml as specified by you. my goal is to generate the html test report. I am sending the file to you <?xml version="1.0" ?> <project name="jcoverage.examples" default="run-cactus-test" basedir="."> <target name="-post-compile-test" > <copy todir="${build.web.dir.real}" file="${test.src.dir}/com/nyse/mks/iis/util/cactus-report.xsl" /> <copy todir="${build.classes.dir.real}"> <fileset dir="${build.test.classes.dir}" excludes="\*\*/\*.java"/> </copy> <input message="Which file do you want to run?" validargs="TestBSVDateUtil" addproperty="testfile"/> <antcall target="run-cactus-test" /> </target> <target name="clean" description="clean up build artefacts"> <delete quiet="true"> <fileset dir="${build.dir}"/> <fileset dir="${basedir}"> </fileset> </delete> </target> <target name="run-cactus-test" depends="run-display-test"/> <target name="run-display-test" > <nbbrowse url="http://localhost:9080/BSVTestCase/ServletTestRunner?suite=com.nyse.mks.iis.util.${testfile)ampxsl=cactus-report.xsl"/> </target> </project> I am getting the error like Buildfile: C:\\Documents and Settings\\laxman\\Desktop\\testproject\\BSVTestCase\\WebContent\\WEB-INF\\lib\\build.xml run-display-test: [nbbrowse] BUILD FAILED: file:C:/Documents and Settings/laxman/Desktop/testproject/BSVTestCase/WebContent/WEB-INF/lib/build.xml:29: Could not create task or type of type: nbbrowse. Ant could not find the task or a class this task relies upon. This is common and has a number of causes; the usual solutions are to read the manual pages then download and install needed JAR files, or fix the build file: - You have misspelt 'nbbrowse'. Fix: check your spelling. - The task needs an external JAR file to execute and this is not found at the right place in the classpath. Fix: check the documentation for dependencies. Fix: declare the task. - The task is an Ant optional task and optional.jar is absent Fix: look for optional.jar in ANT_HOME/lib, download if needed - The task was not built into optional.jar as dependent libraries were not found at build time. Fix: look in the JAR to verify, then rebuild with the needed libraries, or download a release version from apache.org - The build file was written for a later version of Ant Fix: upgrade to at least the latest release version of Ant - The task is not an Ant core or optional task and needs to be declared using <taskdef>. Remember that for JAR files to be visible to Ant tasks implemented in ANT_HOME/lib, the files must be in the same directory or on the classpath Please neither file bug reports on this problem, nor email the Ant mailing lists, until all of these causes have been explored, as this is not an Ant bug. Total time: 1 second please let me if anything wrong.

Posted by laxman on July 25, 2005 at 08:22 PM PDT #

please send me the complete build.xml file I did some modifications on the build file and mapped according to my file. I copied the "cactus-report.xsl" into my workspace with out any modifications Is there any modifications required in that file. and I did the changes in web.xml as specified by you. my goal is to generate the html test report. I am sending the file to you <?xml version="1.0" ?> <project name="jcoverage.examples" default="run-cactus-test" basedir="."> <target name="-post-compile-test" > <copy todir="${build.web.dir.real}" file="${test.src.dir}/com/nyse/mks/iis/util/cactus-report.xsl" /> <copy todir="${build.classes.dir.real}"> <fileset dir="${build.test.classes.dir}" excludes="\*\*/\*.java"/> </copy> <input message="Which file do you want to run?" validargs="TestBSVDateUtil" addproperty="testfile"/> <antcall target="run-cactus-test" /> </target> <target name="clean" description="clean up build artefacts"> <delete quiet="true"> <fileset dir="${build.dir}"/> <fileset dir="${basedir}"> </fileset> </delete> </target> <target name="run-cactus-test" depends="run-display-test"/> <target name="run-display-test" > <nbbrowse url="http://localhost:9080/BSVTestCase/ServletTestRunner?suite=com.nyse.mks.iis.util.${testfile)ampxsl=cactus-report.xsl"/> </target> </project> I am getting the error like Buildfile: C:\\Documents and Settings\\laxman\\Desktop\\testproject\\BSVTestCase\\WebContent\\WEB-INF\\lib\\build.xml run-display-test: [nbbrowse] BUILD FAILED: file:C:/Documents and Settings/laxman/Desktop/testproject/BSVTestCase/WebContent/WEB-INF/lib/build.xml:29: Could not create task or type of type: nbbrowse. Ant could not find the task or a class this task relies upon. This is common and has a number of causes; the usual solutions are to read the manual pages then download and install needed JAR files, or fix the build file: - You have misspelt 'nbbrowse'. Fix: check your spelling. - The task needs an external JAR file to execute and this is not found at the right place in the classpath. Fix: check the documentation for dependencies. Fix: declare the task. - The task is an Ant optional task and optional.jar is absent Fix: look for optional.jar in ANT_HOME/lib, download if needed - The task was not built into optional.jar as dependent libraries were not found at build time. Fix: look in the JAR to verify, then rebuild with the needed libraries, or download a release version from apache.org - The build file was written for a later version of Ant Fix: upgrade to at least the latest release version of Ant - The task is not an Ant core or optional task and needs to be declared using <taskdef>. Remember that for JAR files to be visible to Ant tasks implemented in ANT_HOME/lib, the files must be in the same directory or on the classpath Please neither file bug reports on this problem, nor email the Ant mailing lists, until all of these causes have been explored, as this is not an Ant bug. Total time: 1 second please let me if anything wrong.

Posted by laxman on July 25, 2005 at 08:26 PM PDT #

Laxman,

1. No modifications are necessary to cactus-report.xsl. However, if you want to, for example, increase the font size of the report, you would need to change something in cactus-report.xsl. But, in order to get everything working, no changes are necessary.

2. nbbrowse only works within NetBeans IDE. Since you are not using NetBeans IDE, but -- in your own words -- "web sphere application developer", nbbrowse will not work. Instead, you will have to start the browser by using the <tt>exec</tt> task, as described -- for example -- here: Structuring Ant Targets in the NetBeans IDE.

Hope this helps, if not, please leave another comment!

Posted by Geertjan on July 25, 2005 at 11:35 PM PDT #

Thanking you please could you tell me how do I use the exec command in build.xml and how to call the cactus-report.xsl in build.xml. how do I specify the url path in exec. please help me, if possible with any of the example

Posted by laxman on July 26, 2005 at 12:26 AM PDT #

This should work (just replace <tt>c:\\mozilla.exe</tt> below with the full path to your browser):

<target name="run-cactus-test" description="run-deploy,run-display-test">
  <exec executable="c:\\mozilla.exe">
     <arg value="http://localhost:8084/CactusTest/ServletTestRunner?suite=org.me.hello.${testfile}&amp;xsl=cactus-report.xsl"/>
  </exec>
</target>

Posted by Geertjan on July 26, 2005 at 12:33 AM PDT #

I had specified the ie in executable <exec executable="C:\\Program Files\\Internet Explorer\\iexplore.exe"> how do I call the cactus-report.xsl in this <arg value="http://localhost:8090/${project.name}/${client.urlPart}"/>

Posted by guest on July 26, 2005 at 12:35 AM PDT #

<tt>cactus-report.xsl</tt> is called in <tt><arg value</tt> in the <tt>run-cactus-test</tt>.

Posted by Geertjan on July 26, 2005 at 12:40 AM PDT #

I tried with the following modifications in the build.xml file. <?xml version="1.0" ?> <project name="xyz" default="run-test" basedir="."> <target name="run-test" depends="run-cactus-test"> <copy todir="${build.web.dir.real}" file="${test.src.dir}/com/nyse/mks/iis/util/cactus-report.xsl" /> <copy todir="${build.classes.dir.real}"> <fileset dir="${build.test.classes.dir}" excludes="\*\*/\*.java"/> </copy> <input message="Which file do you want to run?" validargs="TestBSVDateUtil" addproperty="testfile"/> <antcall target="run-display-test" /> </target> <target name="run-display-test" depends="run-cactus-test"/> <target name="run-cactus-test" > <exec executable="C:\\Program Files\\Internet Explorer\\iexplore.exe"> <arg value="http://localhost:9080/BSVTestCase/ServletTestRunner?suite=com.nyse.mks.iis.util.${testfile}ampxsl=cactus-report.xsl"/> </exec> </target> </project> but it going to the browser but it is showing the file not found error. please let me know how to resolve this I am very thank full to your for your great coperation from the beggning.

Posted by laxman on July 26, 2005 at 12:51 AM PDT #

Hi Laxman, no problem -- glad to help. Not sure what the problem is. What server are you using? What port number? What host? My assumption is that you're using Tomcat, that your host is 'localhost' and that your port number is 8084.

Posted by Geertjan on July 26, 2005 at 12:53 AM PDT #

I am using websphere5.1 and port numbere is 9080. Could you please tell how do we call the xsl and testcase in exec. I had specied like <exec executable="C:\\Program Files\\Internet Explorer\\iexplore.exe"> <arg value="http://localhost:9080/BSVTestCase/ServletTestRunner?suite=com.nyse.mks.iis.util.${TestBSVDateUtil}ampxsl=cactus-report.xsl"/> </exec> how do I invoke the TestBSVDateUtil test case in cactus-report.xsl I suppose these two lines have the problem.

Posted by laxman on July 26, 2005 at 02:08 PM PDT #

Hi I got the solution to the problem that I mensioned that is generating the test report using junit in cactus I am now looking for the new problem is like can we automate the creation of test cases. does any tool is providing such a feature. Because it consumes devlopers time to invest on writing the test cases. if so please let me know ahout that Thanks and Regards laxman

Posted by laxman on July 31, 2005 at 08:41 PM PDT #

Hi Laxman, good to know you worked it out! If you want to automate the creation of test cases, you can use NetBeans IDE. NetBeans IDE includes templates for JUnit tests. However, templates for Cactus tests are currently not provided. But I think the tests are quite similar -- if you use the IDE to generate a JUnit test, it shouldn't be hard to change it to a Cactus test.

Posted by Geertjan on July 31, 2005 at 08:47 PM PDT #

It nice to see your responce If suppose I am using the netbeans can I automate the create of test cases. If so can please send me one example how to do it , I will try the similar one in cactus. please provide me some example.

Posted by laxman on August 01, 2005 at 12:27 AM PDT #

Just install the IDE, then create a new project, choose File > New File, and choose Empty Test or Test for Existing Class from the JUnit category. Then NetBeans creates a JUnit test for you.

Posted by Geertjan on August 01, 2005 at 02:19 AM PDT #

I am trying to get Cactus integrated with Netbeans and Application Server 8.1. Unfortunately I run into the following exception when trying to run a test: org.apache.cactus.util.ChainedRuntimeException: Missing Cactus property [cactus.contextURL] at org.apache.cactus.internal.configuration.BaseConfiguration.getContextURL(BaseConfiguration.java:59) Any ideas? Thanks.

Posted by Atticus Finch on September 04, 2005 at 06:55 AM PDT #

Got the "Missing Cactus property" problem sorted. It was a matter of not copying the library files to the container library, instead I just had them kept local to the project. However, I've unfortunately come across a new problem, I can't seem to get the web browser to find the XSL stylesheet, even when adding the -post-compile-test target and copy directives to build.xml. Any ideas? Thanks.

Posted by Atticus Finch on September 04, 2005 at 12:44 PM PDT #

Hi Atticus, thanks for your question (and congratulations for sorting out your previous one)! I think your problem is with the run-display-test target. First, there are some questions and answers about this target higher up, in response to questions from pmoc. Secondly, if that doesn't help, could you post your target here? I'll have a look at it. Thanks.

Posted by Geertjan on September 04, 2005 at 03:13 PM PDT #

Cheers for your quick reply Geertjan! Yeah I noticed the & problem myself, Netbeans wouldn't even start compiling without it. Actually at the moment I am just trying to get it to work by manually invoking the URL in my browser like this: http://localhost:8080/CactusTest/ServletTestRunner?suite=my.test.mockservice.MockWebServiceImplTest&xsl=cactus-report.xsl and my targets are: <target name="-post-compile-test" depends="run"> <copy todir="${build.web.dir.real}" file="${test.src.dir}/grid/broker/mockservice/cactus-report.xsl" /> <copy todir="${build.classes.dir.real}"> <fileset dir="${build.test.classes.dir}" excludes="\*\*/\*.java"/> </copy> </target> Thanks again!

Posted by Atticus Finch on September 04, 2005 at 05:56 PM PDT #

Hi Atticus, Sorry but your targets don't seem to have displayed correctly. What exactly is the problem at the moment; I mean, what's going wrong? If you have Ant targets to show me, you can also send them to geertjan.wielenga@sun.com and I'll respond here (so that others too, etc.)

Posted by Geertjan on September 05, 2005 at 01:33 AM PDT #

The targets are in the HTML source, only of course the web browser will not display them as it thinks they are markup tags! Sorry about that, I'll try again below:
<target name="-post-compile-test" depends="run"< <copy todir="${build.web.dir.real}" file="${test.src.dir}/grid/broker/mockservice/cactus-report.xsl" /< <copy todir="${build.classes.dir.real}"< <fileset dir="${build.test.classes.dir}" excludes="\*\*/\*.java"/< </copy< </target<
The problem I have is that when I invoke the
http://localhost:8080/CactusTest/ServletTestRunner?suite=my.test.mockservice.MockWebServiceImplTest&xsl=cactus-report.xsl
URL, the returned test result contains an invalid path to the XSL stylesheet. Probably this is because cactus-report.xsl is not copied to the correct location?

Posted by Atticus Finch on September 05, 2005 at 09:27 AM PDT #

Hi all, am a cactus newbie and run my tests using the browser i use /webapp/ServletTestRunner?.... and my test runs fine but returns xml. I have tried all the suggestions given for deploying the cactus-report.xsl but without success. Can someone tell me where to put it and how to configure web.xml to reflect this location and also what the new test url would be? Regards Elisha

Posted by EEzeugoh on September 26, 2005 at 06:42 PM PDT #

Hi Elisha, put the cactus-report.xsl where it is shown in the screenshot at the start of this blog entry. And web.xml should be configured as shown in step 2 in the instructions above. If you still experience problems, please come back and say exactly what the error message is.

Posted by Geertjan on September 26, 2005 at 06:49 PM PDT #

Hi Geertjan, I have done as you said but still cannot get the transform to work, as it still displays in the browser as an xml document. I used the following URLs without success. http://130.1.19.29:8988/enews2/ServletTestRunner? suite=com.nhbc.osp.test.EnewsTests&transform=yes http://130.1.19.29:8988/enews2/ServletTestRunner?suite=com.nhbc.osp.test.EnewsTests http://130.1.19.29:8988/enews2/ServletTestRunner?suite=com.nhbc.osp.test.EnewsTests&xsl=cactus-report.xsl - This attempts to locate the stylesheet in http://130.1.19.29:8988/enews2/cactus-report.xml. Thanks Elisha PS: The cactus-report.xsl is in the same location as the test cases.

Posted by EEzeugoh on September 26, 2005 at 08:13 PM PDT #

Can you put your <tt>run-display-test</tt> target in an e-mail and send it to my address:

geertjan.wielenga@sun.com

I'll have a look at it and let you know in this blog. I think I know what the problem might be, but if you paste the target in a comment in this blog, it will not display correctly.

Posted by Geertjan on September 26, 2005 at 08:17 PM PDT #

Hi, I’m trying to generate a HTML report of CACTUS Unit Testing thro’ ANT script. Any idea??? I’m able to generate report from a browser using…. http://url:port/WEBAPP_NAME/ServletTestRunner?suite=AllTests&xsl=cactus-report.xsl But I need to do generate it & to be mailed to xyz person thro’ ANT task. Helpppppppppppppppppppppppppppppppppppppp………………………………….

Posted by Shailendra on April 12, 2006 at 09:57 PM PDT #

Hi Laxman, I have a question for u, as you I can c that you too faced the same problem as I'm facing now. My build.xml has this entry: <target name="run-display-test" depends="run-deploy"> <exec executable="C:\\Software\\Exes\\mozilla-win32-1.7.13-installer.exe"> <arg value="http://localhost:8080/CactusTest/ServletTestRunner?suite=com.bms.baycare.controller.news.${testfile}&amp;xsl=cactus-report.xsl"/> </exec> </target> do u c a problem with this??? When I run the project, its opening the index.jsp page n not the unit test report. ~Vikram

Posted by vikram on April 14, 2007 at 11:24 PM PDT #

Hi, can u make it for netbeans 6.5 with glassfish v2?

i really need to learn how to use cactus. Thanks anyway.

Posted by Sergio on March 31, 2009 at 01:55 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« July 2014
SunMonTueWedThuFriSat
  
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today