JSFUnitWithAnt

The jsfunitwar Ant task

JSFUnit provides an Ant task that takes your WAR and "jsfunifies" it.  That is, it adds filters and servlets to the web.xml and it adds jars to WEB-INF/lib.  Here are the attributes for the jsfunitwar task:

srcfile - A path to a WAR file or a directory.  If a directory then it is assumed that you are using an exploded WAR and the result will be another exploded WAR.

destfile - The name and location of the resulting "jsfunified" WAR.  If destfile is a directory then you will end up with an exploded WAR.

autoaddjars - If set to true then jsfunitwar will find all the jars it needs in your classpath and it will add them to WEB-INF/lib.  If it can not find a jar then it will print a warning message and continue.

container - For JBoss 5, you need to leave out XercesImpl and Xalan.  jsfunitwar will not add those jars if you specify "jboss5x" for the container attribute.

 

The jsfunitwar element also processes these child elements.

  • A regular ant <classes> element that specifies where your test classes are. 
  • A regular ant <lib> element specifies any additional jars you would like to add.
  • <TestRunner/>, which just adds the ServletTestRunner to your web.xml.  This allows you to run your tests from a browser.  If you will only run your tests from JUnit then you can leave this out.  But it never hurts to add it.

Integrating JSFUnit with Ant requires a few steps.

 

  • Build your application into a war structure. (exploded directory or .war file)

    • For now we will not cover this step as it is project dependant.

  • Run the jsfunitwar task over the war created in step 1

    • Here is an example of how to do this

<target name="create.jsfunit.exploded.dir" depends="compile.test">
     <path id="jsfunit.classpath">
         <fileset dir="${lib.dir}/jsf-unit">
          <include name="**/*.jar"></include>
         </fileset>
         <fileset dir="${lib.dir}/junit">
          <include name="**/*.jar"></include>
         </fileset>
         <fileset dir="${lib.dir}/aspectj">
          <include name="**/*.jar"></include>
         </fileset>
     </path>

     <taskdef
         name="jsfunitwar"
         classname="org.jboss.jsfunit.ant.JSFUnitWarTask"
         classpathref="jsfunit.classpath"></taskdef>

     <jsfunitwar srcfile="${exploded.dir}"
              destfile="${exploded-jsfunit.dir}"
                    autoaddjars="true">
          
          <!-- This is where you have compiled your jsfunit tests -->
          <classes dir="${classes.test}"
               includes="**/jsfunit/**/*.class">
          </classes>
          
          <TestRunner></TestRunner>
     </jsfunitwar>
</target>
  • Deploy the newly created war file/folder to your server

    • Once again this is project specific and will not be covered by this wiki topic.

  • Run the jsf unit tests

    • Here is an example ant target for running the tests. Obviously some things here you will need to change or remove. But this is the general idea.

<target name="run.full.suite">
    <mkdir dir="${junit.xml.dir}" ></mkdir>

    <junit
        showoutput="yes"
        fork="yes"
        printsummary="yes"
        haltonfailure="yes"
        haltonerror="yes">

        <classpath>
            <fileset dir="${lib.dir}">
                <include name="**/*.jar" ></include>
            </fileset>
            <pathelement location="${classes.test}" ></pathelement>
            <pathelement location="${classes.prod}" ></pathelement>
        </classpath>

        <jvmarg value="-Dcactus.contextURL=http://${tomcat.hostname}:${tomcat.port}/${tomcat.gekko.webapp}" ></jvmarg>

        <formatter type="xml" usefile="yes" ></formatter>

        <test name="gekko.web.jsfunit.GekkoWebJSFFullSuite"
            todir="${junit.xml.dir}"
            outfile="${junit.report.file}" ></test>
    </junit>


    <junitreport
        todir="${junit.xml.dir}"
        tofile="${junit.report.file}">
        
        <fileset dir="${junit.xml.dir}">
            <include name="*.xml" ></include>
        </fileset>
        
        <report
            todir="${junit.xml.dir}"
            styledir="${lib.dir}/jsf-unit"
            format="noframes" ></report>
    </junitreport>
    
</target>

 

You may consider the following links useful.

 

JBoss.com - Forums - JSFUnitwar Ant Task

 

JBoss.com - Forums - JSFUnit/Ant/Cargo

 

Also, in the JSFUnit example code, there is a Maven pom that "JSFUnifies" a WAR and runs JSFUnit tests against that WAR.  You can browse that example and its POM here:

http://fisheye.jboss.org/browse/JSFUnit/trunk/jboss-jsfunit-examples/jboss-jsfunit-examples-hellojsf/jboss-jsfunit-examples-hellojsf-ant