Junit report does not generate a report if there is an approval error Class for testing
public class Math {
static public int add(int a, int b) {
return a + b;
}
static public int multiply ( int a, int b) {
if(a==0&&b==0)
return 0;;
return a * b;
}
}
Junit Testing Class
import junit.framework.*;
public class TestMath extends TestCase {
protected void setUp() {
}
protected void tearDown() {
}
public void testAdd() {
int num1 = 3;
int num2 = 2;
int total = 5;
int sum = 0;
sum = Math.add(num1, num2);
assertEquals(sum, total);
}
public void testMulitply() {
int num1 = 3;
int num2 = 7;
int total = 21;
int sum = 0;
sum = Math.multiply(num1, num2);
assertEquals("Problem with multiply", sum, total);
num1 = 5;
num2 = 4;
total = 20;
sum = Math.multiply(num1, num2);
assertEquals("Problem with multiply", sum, total);
}
public void testv(){
Assert.assertEquals(1, Math.multiply(0, 0));
}
}
I am using junit report to create reports
The problem is that if the statement prevents reports from being generated, Ant Build crashes. The assumed value of MY is that if the statement fails, it should be indicated in failures, and not as a result of a build error.
Ant XML
<project name="SampleJUnitTests" default="dist" basedir=".">
<description>
Sample JUnit Tests
</description>
<property name="project_name" value="junitSamples"/>
<property name="src" location="src"/>
<property name="build" location="bin"/>
<property name="dist" location="dist"/>
<property name="lib" location="lib"/>
<property name="res" location="res"/>
<property name="reports" location="reports"/>
<property name="jar_name" value="${project_name}.jar"/>
<property name="war_name" value="${project_name}.war"/>
<target name="compile" depends="init" description="compile the source code " >
<javac srcdir="${src}" destdir="${build}">
<classpath>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</classpath>
</javac>
</target>
<target name="dist" depends="compile" description="generate the distributable files " >
<jar jarfile="${dist}/${jar_name}" basedir="${build}"/>
</target>
<target name="clean"
description="clean up" >
<delete dir="${build}"/>
<delete dir="${dist}"/>
<delete dir="${reports}"/>
</target>
<target name="run-tests" depends="compile" description="run your test suite" >
<junit printsummary="yes" haltonfailure="yes" showoutput="yes" >
<classpath>
<pathelement path="${build}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</classpath>
<batchtest fork="yes" todir="${reports}/raw/">
<formatter type="xml"/>
<fileset dir="${src}">
<include name="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>
</target>
<target name ="test" depends="run-tests">
<junitreport todir="${reports}">
<fileset dir="${reports}/raw/">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${reports}/html/"/>
</junitreport>
</target>
<target name ="run" depends="" description="if this project can be run, run it" >
</target>
<target name="init" description="initialize the build environment" >
<tstamp/>
<mkdir dir="${build}"/>
<mkdir dir="${lib}"/>
<mkdir dir="${dist}/lib"/>
<mkdir dir="${reports}"/>
<mkdir dir="${reports}/raw/"/>
<mkdir dir="${reports}/html/"/>
</target>
<target name="all" depends="clean, test">
</target>
</project>
MY requirement - if the statement does not work, should it show up on error? What do I need to do?
source
share