Running 'assertEquals' method in Junit Test From Ant

In this example assertEquals (excepted, actual) method are used
to test the application with the JUnit framework, this unit tests are possible
by extend the junit.framework.TestCases class. JUnit
and Ant are necessary tools for Java development. JUnit provide methods to easily assert things about your own classes, as well as
the ability to run a group of tests. The requirements of writing unit test
classes are described below, with an example. The another method as same
as:
- assertEquals (message,
expected, actual)
- assertEquals (expected, actual, delta)
- assertEquals (message, expected, actual, delta
JunitTest.java
package JUnit;
import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.Math;
import junit.framework.*;
import junit.textui.TestRunner;
public class JunitTest extends TestCase{
public void addition() {
int a = 3;
int b = 4;
int expected = 7;
int add = a + b;
int actual = add;
assertEquals("adding 3 and 4", expected, actual);
}
public static Test suite(){
TestSuite suite = new TestSuite("addition");
return suite;
}
public static void main(String args[]){
TestRunner.run( suite() );
}
}
|
build.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="JUnitTesting" default="run" basedir=".">
<property file="build.properties"/>
<property name="src.dir" value="source"/>
<property name="class.dir" value="./classes"/>
<path id="proj.class.path">
<fileset dir="lib">
<include name="**/*.zip"/>
</fileset>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
<pathelement location="${class.dir}"/>
</path>
<target name="clean">
<delete dir="${class.dir}" failonerror="true" />
<mkdir dir="${class.dir}"/>
</target>
<target name="compile" depends="clean">
<javac destdir="${class.dir}" debug="on"
deprecation="false">
<src path="${src.dir}"/>
<classpath refid="proj.class.path"/>
</javac>
</target>
<target name="run" depends="compile">
<java classname="JUnit.JunitTest"
classpathref="proj.class.path" fork="yes" />
</target>
</project>
|
Output:
Download Source Code