1. Install ant {something like C:\Junit\Ant\apache-ant-1.8.4-bin\apache-ant-1.8.4 say it ANT_HOME}
2, Install Junit {remember to set classpath example C:\Junit\junit4.8.1\junit4.8.1 in classpath}
3. copy junit.x.x.jar in ANT_HOME/lib
4. Now you working folder ex C:\Junit\Ant_project
5. Create test and src folder in it and put .java file in src folder
6. example file 1:
public class Subscription {
private int price ; // subscription total price in euro-cent
private int length ; // length of subscription in months
// constructor :
public Subscription(int p, int n) {
price = p ;
length = n ;
}
/**
* Calculate the monthly subscription price in euro,
* rounded up to the nearest cent.
*/
public double pricePerMonth() {
double r = (double) price / (double) length ;
return r ;
}
/**
* Call this to cancel/nulify this subscription.
*/
public void cancel() { length = 0 ; }
}
File 2:
import org.junit.* ;
import static org.junit.Assert.* ;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SubscriptionTest {
@Test
public void test_returnEuro() {
System.out.println("Test if pricePerMonth returns Euro...") ;
Write_To_File(12,"hi","bye");
Subscription S = new Subscription(200,2) ;
assertTrue(S.pricePerMonth() == 1.0) ;
Write_To_File(11,"hi","bye");
}
@Test
public void test_roundUp() {
System.out.println("Test if pricePerMonth rounds up correctly...") ;
Write_To_File(10,"hi","bye");
Subscription S = new Subscription(200,3) ;
assertTrue(S.pricePerMonth() == 0.67) ;
}
@Test
public void test_absolute() {
System.out.println("shyam") ;
Write_To_File(10,"hi","bye");
Subscription S = new Subscription(200,4) ;
assertTrue(S.pricePerMonth() == 50.0) ;
}
public void Write_To_File(int var, String str, String str1) {
try {
FileWriter fstream = new FileWriter("C:\\Junit\\Result\\out.txt",true);
System.out.println("jhansi");
BufferedWriter fbw = new BufferedWriter(fstream);
fbw.write("Our append txt...");
fbw.newLine();
fbw.close();
}
catch (Exception ex) {
System.out.println("Exception Occured:: ");
ex.printStackTrace();
}
}
}
7. Now craete Build.xml in C:\Junit\Ant_project
8. copy paste following xml in build.xml (Please replace < and > sing in the following xml)
lessthanSign ?xml version="1.0"?>
lessthanSign project name="tutorial" default="build" basedir=".">
lessthanSign property name="srcdir" value="./src" />
lessthanSign property name="testdir" value="./test" />
lessthanSign property name="lib" value="./lib" />
lessthanSign property name="classes" value="./classes" />
lessthanSign target name="build">
lessthanSign javac srcdir="." includeantruntime="false" includes="**/*.java"/>
lessthanSign /target>
lessthanSign target name="ensure-test-name" unless="test">
lessthanSign fail message="You must run this target with -Dtest=TestName"/>
lessthanSign /target>
lessthanSign path id="classpath.base"/>
lessthanSign path id="classpath.test">
lessthanSign pathelement location="C:\Junit\junit4.8.1\junit4.8.1\junit-4.8.1.jar" />
lessthanSign pathelement location="${testdir}" />
lessthanSign pathelement location="${srcdir}" />
lessthanSign path refid="classpath.base" />
lessthanSign /path>
lessthanSign target name="clean" >
lessthanSign delete verbose="${full-compile}">
lessthanSign fileset dir="${testdir}" includes="**/*.class" />
lessthanSign /delete>
lessthanSign /target>
lessthanSign target name="compile" depends="clean">
lessthanSign javac srcdir="${testdir}" destdir="${testdir}" verbose="${full-compile}" >
lessthanSign classpath refid="classpath.test"/>
lessthanSign /javac>
lessthanSign /target>
lessthanSign target name="test" depends="compile">
lessthanSign junit>
lessthanSign classpath refid="classpath.test" />
lessthanSign formatter type="brief" usefile="false" />
lessthanSign test name="test.SubscriptionTest" haltonfailure="no" outfile="result1"/>
lessthanSign /junit>
lessthanSign /target>