About JUnit Framework
What is JUnit ?
Unit test & Integration test
Unit test using main() method
public class LinesInPage {
private int page;
public void setPage(int page) {
this.page = page;
}
public int getStartLine(){
return page-1*10+1;
}
public int getEndLine(){
return page*10;
}
public void main(String[] args){
LinesInPage lp = new LinesInPage ();
lp .setPage(3);
int start = lp .getStart();
int end = lp .getEnd();
if(start != 21){
System.out.println("Test Fail, Expected: 21, But "+start);
}else{
System.out.println("Test OK !");
}
}
}
Unit test using another class
public class LinesInPageTest {
public void main(String[] args){
LinesInPage lp = new LinesInPage();
lp .setPage(3);
int start = lp .getStart();
int end = lp .getEnd();
if(start != 21){
System.out.println("Expected: 21, But "+start);
}else{
System.out.println("Test OK !");
}
}
}
JUnit Architecture
JUnit simple example
public class Math {
public static int add(int a, int b) {
return a+b;
}
public static int sub(int a, int b) {
return a-b;
}
}
import junit.framework.*;
public class MathTest extends TestCase {
public void testAdd() {
int num1 = 3;
int num2 = 2;
int sum = Math.add(num1, num2);
assertEquals(sum, 5); // OK
}
public void testSub() {
int num1 = 3;
int num2 = 2;
int diff = Math.sub(num1, num2);
assertEquals(diff, 5); // Fail
}
}
package junittest;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class MathTest02 extends TestCase {
public MathTest02() {}
public void testAdd() {
int num1 = 3;
int num2 = 2;
int sum = junittest.Math.add(num1, num2);
assertEquals(sum, 1);
}
public void testSub() {
int num1 = 3;
int num2 = 2;
int diff = junittest.Math.sub(num1, num2);
assertEquals(diff, 5);
}
public static Test suite(){
return new TestSuite(MathTest02.class);
}
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
}
}
A TestSuite is a Composite of Tests. It runs a collection of test cases. Here is an example using the dynamic test definition.
TestSuite suite= new TestSuite();
suite.addTest(new MathTest("testAdd"));
suite.addTest(new MathTest("testDivideByZero"));
Alternatively, a TestSuite can extract the tests to be run automatically. To do so you pass the class of your TestCase class to the TestSuite constructor.
TestSuite suite = new TestSuite(MathTest.class);
This constructor creates a suite with all the methods starting with "test" that take no arguments.
package junittest;
import junit.framework.*;
public class MathTest03 extends TestCase {
public MathTest03() {}
public MathTest03(String name) {
super(name);
}
public void testAdd() {
int num1 = 3;
int num2 = 2;
int sum = junittest.Math.add(num1, num2);
assertEquals(sum, 1);
}
public void testSub() {
int num1 = 3;
int num2 = 2;
int diff = junittest.Math.sub(num1, num2);
assertEquals(diff, 5);
}
public static Test suite(){
TestSuite suite = new TestSuite();
Test t1 = new MathTest03("testAdd");
Test t2 = new MathTest03("testSub");
suite.addTest(t1);
suite.addTest(t2);
return suite;
}
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
// Under JUnit version 3.8, awtui, swingui package available
//junit.awtui.TestRunner.run(suite());
//junit.swingui.TestRunner.run(suite());
}
}
TestCase Life Cycle
setUp
testXXX()
tearDown()
Repeats 1~3 for each testXXX() method....
Life Cycle methods example
JUnit & Exception
public class Math
{
public static int add(int a, int b)
{
boolean bool = true;
if(bool) throw new RuntimeException();
return a+b;
}
public static int sub(int a, int b)
{
return a-b;
}
}
JUnit in Eclipse
JUnit with Ant in Eclipse
Two great tastes that taste great together
You're building with Ant already, now test with it too!
Facilitates continuous integration and testing
Richer and more flexible reporting than otherwise possible, including publishing and e-mailing
Fail builds if tests fail
JUnit Report with Ant in Eclipse
<project name="JUnitTest" default="junitreport" basedir=".">
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="report.dir" value="${build.dir}/junitreport"/>
<property name="main-class" value="junittest.MathTest03"/>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile" depends="clean">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}/junittest" destdir="${classes.dir}"/>
</target>
<target name="run" depends="compile">
<java fork="true" classname="${main-class}">
<classpath>
<path id="application" location="${classes.dir}"/>
</classpath>
</java>
</target>
<target name="junit" depends="run">
<mkdir dir="${report.dir}"/>
<junit printsummary="yes">
<classpath>
<path id="application" location="${classes.dir}"/>
</classpath>
<formatter type="xml"/>
<batchtest fork="yes" todir="${report.dir}">
<fileset dir="${classes.dir}" includes="**/*Test03.class"/>
</batchtest>
</junit>
</target>
<target name="junitreport" depends="junit">
<junitreport todir="${report.dir}">
<fileset dir="${report.dir}" includes="TEST-*.xml"/>
<report todir="${report.dir}"/>
</junitreport>
</target>
</project>
Apache Ant/Fileset
From Wikibooks, the open-content textbooks collection
FileSets are ant's way of creating groups of files to do work on. These files can be found in a directory tree starting in a base directory and are matched by patterns taken from a number of PatternSets and Selectors. FileSets can appear inside tasks that support this feature or at the same level as target - i.e., as children of project.
PatternSets can be specified as nested <patternset> elements. In addition, FileSet holds an implicit PatternSet and supports the nested <include>, <includesfile>, <exclude> and <excludesfile> elements of PatternSet directly, as well as PatternSet's attributes.
Selectors are available as nested elements.within the FileSet. If any of the selectors within the FileSet do not select the file, the file is not considered part of the FileSet. This makes FileSets equivalent to an <and> selector container.
Wildcards
Wildcards are used by ant to specify groups of files that have a pattern to their names.
- ? is used to match any character.
- * is used to match zero or more characters.
- ** is used to match zero or more directories.
Examples
<fileset dir="${server.src}" casesensitive="yes">
<include name="**/*.java"/>
<exclude name="**/*Test*"/>
</fileset>
Groups all files in directory ${server.src} that are Java source files and don't have the text Test in their name.
<fileset dir="${server.src}" casesensitive="yes">
<patternset id="non.test.sources">
<include name="**/*.java"/>
<exclude name="**/*Test*"/>
</patternset>
</fileset>
<fileset dir="${client.src}"> <patternset refid="non.test.sources"/> </fileset>
<fileset dir="${server.src}" casesensitive="yes"> <filename name="**/*.java"/> <filename name="**/*Test*" negate="true"/> </fileset>
<fileset dir="${server.src}" casesensitive="yes"> <filename name="**/*.java"/> <not> <filename name="**/*Test*"/> </not> </fileset>