By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,939 Members | 1,600 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,939 IT Pros & Developers. It's quick & easy.

JUnit Report using ANT. I NEED HELP!!!!

P: 1
Hi, I'm currently working on getting junit and junireport to take some java files, convert it to xml and then display error messages / success rates etc in html which is one of the features of junitreport. I created a simple Math.java class which contains:

package test;

public class Math {

public int add(int a, int b) {

return a + b;
}

public int multiply(int i, int j) {

return i * j;
}


public int divide(int i, int j) {

return i / j;
}


public int subtract(int i, int j) {

return i - j;
}


public int Calculator () {

return 12;


}

}


... and a simple TestMath class:

package test;

import junit.framework.TestCase;

public class TestMath extends TestCase {

// public static void main (String [] args) {
// junit.textui.TestRunner.run(TestMath.class);
// junit.swingui.TestRunner.run(TestMath.class);
// }


Math nm = null;
Math nt = null;
Math nd = null;
Math ns = null;



protected void setUp() throws Exception {

nm = new Math();
nt = new Math();
nd = new Math();
ns = new Math();

}

protected void tearDown() throws Exception {


nm = null;
nt = null;
nd = null;
ns = null;

}


//Tests for Addition
public void testAdd() {

assertEquals(3, nm.add(1,2));
}


public void testAdd1() {

assertEquals(10, nm.add(5,5));
}


public void testAdd2() {

assertEquals(20, nm.add(16,4));
}




//Tests for Multiplication
public void testMultiply() {

assertEquals(12, nt.multiply(6,2));
}


public void testmultiply1() {

assertEquals(160, nt.multiply(2,80));
}

//Tests for Division
public void testdivide() {

assertEquals(25, nd.divide(50,2));
}


public void testdivide1() {

assertEquals(12, nd.divide(24,2));
}


//Tests for Subtraction
public void testsubtraction() {

assertEquals(620, ns.subtract(1000,380));
}

public void testsubtraction1() {

assertEquals(500, ns.subtract(1000,500));
}



}

Within ANT i have my junit and junitreport tasks:

<!-- ********************************* -->
<!-- * JUnit / JUnit Reports * -->
<!-- ********************************* -->


<description>
JUnit code to test aspects of the af. Will output .xml files
which will be converted to HTML format using junitreport.
</description>


<!-- top level targets -->


<target name="init" description="initialize the build environment (JUnit)" >
<!-- Create the time stamp -->
<tstamp/>
<!-- Create directory structures -->
<mkdir dir="${build}"/>
<mkdir dir="${lib}"/>
<mkdir dir="${dist}/lib"/>
<mkdir dir="${reports}"/>
<mkdir dir="${reports}/raw/"/>
<mkdir dir="${reports}/html/"/>
<!--mkdir dir="${src}" /-->
</target>

<target name="compile" depends="init" description="compile the source code (JUnit) " >
<javac srcdir="workspace\af\src\test\DJK" destdir="${build}">

<!--move todir="workspace\af\src\test\DJK">
<fileset dir="workspace\af\src\test">
<include name="TestMath.java"/>
<include name="Math.java" />
<exclude name=""/>
</fileset>
</move-->

<classpath>

<fileset dir="lib">

<include name="**/*.jar"/>

</fileset>

</classpath>
</javac>
</target>


<target name="dist" depends="compile" description="generate the distributable files (JUnit) " >

<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/${jar_name}" basedir="${build}"/>

</target>


<target name="cleanfiles" description="clean up files (JUnit)" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
<delete dir="${reports}"/>
</target>


<target name="run-tests" depends="compile" description="run the test suite (JUnit)" >

<junit printsummary="yes" haltonfailure="no" showoutput="yes" >
<classpath>
<pathelement path="${build}"/>
<fileset dir="dist">
<include name="**/*.jar"/>
</fileset>
</classpath>

<batchtest fork="yes" todir="${reports}/raw/">
<formatter type="xml"/>
<fileset dir="workspace\af\src\test\DJK">
<include name="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>
</target>

<target name ="test" depends="run-tests" description="Run Tests (JUnit)">
<junitreport todir="${reports}">
<fileset dir="${reports}/raw/">
<include name="*.xml"/>
</fileset>
<report format="frames" todir="${reports}\html\"/>
</junitreport>
</target>

<target name ="run" depends="" description="if this project can be run, run it (JUnit)" >
</target>

<!-- supporting targets -->

<target name="all" depends="clean, test">

</target>


........This does transform into html but with the following error message:

TestMath

java.lang.ClassNotFoundException: TestMath at java.net.URLClassLoader.findClass(URLClassLoader.j ava:375) at java.lang.ClassLoader.loadClass(ClassLoader.java:5 62) at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:442) at java.lang.ClassLoader.loadClass(ClassLoader.java:4 94) at java.lang.Class.forName1(Native Method) at java.lang.Class.forName(Class.java:180)

....The build is only successful because haltonfailure is set to no!

I know this is a big question, but any help would be very much greatly appreciated.

Danny Knights
ENGLAND.
Sep 15 '06 #1
Share this Question
Share on Google+
3 Replies


P: 1
Hi Guys,
this script is running fine
but is there any option of running JUNIT report for particular method
in a particular test file.

if someone have any idea about it please let me know
Oct 31 '06 #2

P: 1
Hi Danny Knights,
Did you got any solution for the below problem. because i am also facing the same problem. If you got solution please help me..

thanks
kishore


Hi, I'm currently working on getting junit and junireport to take some java files, convert it to xml and then display error messages / success rates etc in html which is one of the features of junitreport. I created a simple Math.java class which contains:

package test;

public class Math {

public int add(int a, int b) {

return a + b;
}

public int multiply(int i, int j) {

return i * j;
}


public int divide(int i, int j) {

return i / j;
}


public int subtract(int i, int j) {

return i - j;
}


public int Calculator () {

return 12;


}

}


... and a simple TestMath class:

package test;

import junit.framework.TestCase;

public class TestMath extends TestCase {

// public static void main (String [] args) {
// junit.textui.TestRunner.run(TestMath.class);
// junit.swingui.TestRunner.run(TestMath.class);
// }


Math nm = null;
Math nt = null;
Math nd = null;
Math ns = null;



protected void setUp() throws Exception {

nm = new Math();
nt = new Math();
nd = new Math();
ns = new Math();

}

protected void tearDown() throws Exception {


nm = null;
nt = null;
nd = null;
ns = null;

}


//Tests for Addition
public void testAdd() {

assertEquals(3, nm.add(1,2));
}


public void testAdd1() {

assertEquals(10, nm.add(5,5));
}


public void testAdd2() {

assertEquals(20, nm.add(16,4));
}




//Tests for Multiplication
public void testMultiply() {

assertEquals(12, nt.multiply(6,2));
}


public void testmultiply1() {

assertEquals(160, nt.multiply(2,80));
}

//Tests for Division
public void testdivide() {

assertEquals(25, nd.divide(50,2));
}


public void testdivide1() {

assertEquals(12, nd.divide(24,2));
}


//Tests for Subtraction
public void testsubtraction() {

assertEquals(620, ns.subtract(1000,380));
}

public void testsubtraction1() {

assertEquals(500, ns.subtract(1000,500));
}



}

Within ANT i have my junit and junitreport tasks:

<!-- ********************************* -->
<!-- * JUnit / JUnit Reports * -->
<!-- ********************************* -->


<description>
JUnit code to test aspects of the af. Will output .xml files
which will be converted to HTML format using junitreport.
</description>


<!-- top level targets -->


<target name="init" description="initialize the build environment (JUnit)" >
<!-- Create the time stamp -->
<tstamp/>
<!-- Create directory structures -->
<mkdir dir="${build}"/>
<mkdir dir="${lib}"/>
<mkdir dir="${dist}/lib"/>
<mkdir dir="${reports}"/>
<mkdir dir="${reports}/raw/"/>
<mkdir dir="${reports}/html/"/>
<!--mkdir dir="${src}" /-->
</target>

<target name="compile" depends="init" description="compile the source code (JUnit) " >
<javac srcdir="workspace\af\src\test\DJK" destdir="${build}">

<!--move todir="workspace\af\src\test\DJK">
<fileset dir="workspace\af\src\test">
<include name="TestMath.java"/>
<include name="Math.java" />
<exclude name=""/>
</fileset>
</move-->

<classpath>

<fileset dir="lib">

<include name="**/*.jar"/>

</fileset>

</classpath>
</javac>
</target>


<target name="dist" depends="compile" description="generate the distributable files (JUnit) " >

<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/${jar_name}" basedir="${build}"/>

</target>


<target name="cleanfiles" description="clean up files (JUnit)" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
<delete dir="${reports}"/>
</target>


<target name="run-tests" depends="compile" description="run the test suite (JUnit)" >

<junit printsummary="yes" haltonfailure="no" showoutput="yes" >
<classpath>
<pathelement path="${build}"/>
<fileset dir="dist">
<include name="**/*.jar"/>
</fileset>
</classpath>

<batchtest fork="yes" todir="${reports}/raw/">
<formatter type="xml"/>
<fileset dir="workspace\af\src\test\DJK">
<include name="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>
</target>

<target name ="test" depends="run-tests" description="Run Tests (JUnit)">
<junitreport todir="${reports}">
<fileset dir="${reports}/raw/">
<include name="*.xml"/>
</fileset>
<report format="frames" todir="${reports}\html\"/>
</junitreport>
</target>

<target name ="run" depends="" description="if this project can be run, run it (JUnit)" >
</target>

<!-- supporting targets -->

<target name="all" depends="clean, test">

</target>


........This does transform into html but with the following error message:

TestMath

java.lang.ClassNotFoundException: TestMath at java.net.URLClassLoader.findClass(URLClassLoader.j ava:375) at java.lang.ClassLoader.loadClass(ClassLoader.java:5 62) at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:442) at java.lang.ClassLoader.loadClass(ClassLoader.java:4 94) at java.lang.Class.forName1(Native Method) at java.lang.Class.forName(Class.java:180)

....The build is only successful because haltonfailure is set to no!

I know this is a big question, but any help would be very much greatly appreciated.

Danny Knights
ENGLAND.
Feb 28 '07 #3

P: 1
Has anyone found an answer to this problem? I too am running into this issue.

Thanks,

Paul

Hi Danny Knights,
Did you got any solution for the below problem. because i am also facing the same problem. If you got solution please help me..

thanks
kishore
Apr 26 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.