X

Geertjan's Blog

  • June 22, 2005

Plant Cactus in NetBeans IDE 4.1

Geertjan Wielenga
Product Manager
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):

Join the discussion

Comments ( 34 )
  • laxman Thursday, July 21, 2005
    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
  • Geertjan Thursday, July 21, 2005
    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.

  • laxman Thursday, July 21, 2005
    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
  • laxman Thursday, July 21, 2005
    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
  • pmoc Saturday, July 23, 2005
    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
  • Geertjan Saturday, July 23, 2005
    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!

  • Geertjan Saturday, July 23, 2005
    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!
  • laxman Tuesday, July 26, 2005

    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.
  • laxman Tuesday, July 26, 2005
    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.
  • Geertjan Tuesday, July 26, 2005
    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!

  • laxman Tuesday, July 26, 2005
    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
  • Geertjan Tuesday, July 26, 2005
    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>
  • guest Tuesday, July 26, 2005
    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}"/>
  • Geertjan Tuesday, July 26, 2005
    <tt>cactus-report.xsl</tt> is called in <tt><arg value</tt> in the <tt>run-cactus-test</tt>.
  • laxman Tuesday, July 26, 2005
    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.
  • Geertjan Tuesday, July 26, 2005
    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.
  • laxman Tuesday, July 26, 2005
    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.
  • laxman Monday, August 1, 2005
    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
  • Geertjan Monday, August 1, 2005
    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.
  • laxman Monday, August 1, 2005
    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.
  • Geertjan Monday, August 1, 2005
    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.
  • Atticus Finch Sunday, September 4, 2005
    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.
  • Atticus Finch Sunday, September 4, 2005
    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.
  • Geertjan Sunday, September 4, 2005
    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.
  • Atticus Finch Monday, September 5, 2005
    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!
  • Geertjan Monday, September 5, 2005
    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.)
  • Atticus Finch Monday, September 5, 2005
    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?
  • EEzeugoh Tuesday, September 27, 2005
    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
  • Geertjan Tuesday, September 27, 2005
    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.
  • EEzeugoh Tuesday, September 27, 2005
    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.
  • Geertjan Tuesday, September 27, 2005
    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.

  • Shailendra Thursday, April 13, 2006
    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………………………………….
  • vikram Sunday, April 15, 2007
    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
  • Sergio Tuesday, March 31, 2009

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

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


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha
Oracle

Integrated Cloud Applications & Platform Services