3. What's JUnit
• A unit test framework for Java
• Originated from xUnit
• An important component in test-driven
development (TDD)
o TDD comes from XP (eXtreme Programming)
o XP is a methodology to realize agile SW
development
Useful when the spec changes frequently.
Test case guarantees your SW quality
when you do refactor.
5. Hierarchy for JUnit (2/2)
• TestRunner
o Use to run the test & get/show the test result
• Test
o Define the interface for test
• TestCase (Implements the Test interface)
o A test case is a test method.
• TestSuite (Implements the Test interface)
o A test suite collects the test methods in TestCase class (the
method name's prefix must be "test")
o A test suite also collects the Tests (TestCase+TestSuite)
• TestResult
o Use to collect the test result.
6. Something you must know before UT
• Test Class
o The class you use to test the tested class.
• Tested Class
o The class you want to test.
To verify the correctness to the tested class,
the test class will invoke the tested class's method.
(Test Class) (Tested Class)
Origin2DPointTest.java Origin2DPoint.java
7. Examples (1/7)
1. TestCase
o A specific test method which is indicated when
invoking the constructor
2. TestSuite
o Collects all the test methods in TestCase
3. TestSuite (1+2)
o Collects the Tests (TestCase + TestSuite)
8. Examples (2/7)
• Directory structure for example
o tw/xxxx/shape/
Point.java
OriginPoint.java
o tw/xxxx/shape/tests
OriginPointTest.java
xxxx
location to junit
• Compile & Run
xxxx
xxxx
xxxx
9. Examples (3/7)
• Point.java
public class Point{
private int x;
private int y;
public Point(int x, int y){
this.x = x;
this.y = y;
}
public Point(){
this(0,0);
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
}
10. Examples (4/7)
• OriginPointTest.java
public class OriginPointTest extends TestCase{ public void testOriginY(){
private Point orignPoint; System.out.println("[testOriginY]");
assertEquals(0, orignPoint.getY());
public OriginPointTest(String name){ System.out.println("[[testOriginY]]");
super(name); }
}
public void prefixIsNotTest_testAbnormal(){
protected void setUp(){ System.out.println("[prefixIsNotTest_testAbnormal]");
System.out.println("[setUp]"); fail();
orignPoint = new OriginPoint(); System.out.println("[[prefixIsNotTest_testAbnormal]]");
System.out.println("[[setUp]]"); }
}
public static void main(String[] args){
protected void tearDown(){ TestCase testCase = new OriginPointTest("prefixIsNotTest_testAbnormal") ;
System.out.println("[tearDown]"); TestSuite testSuite = new TestSuite(OriginPointTest.class);
orignPoint = null; System.out.println("case 1");
System.out.println("[[tearDown]]"); TestRunner.run(testCase);
System.out.println("case 2");
} TestRunner.run(testSuite);
System.out.println("case 3");
public void testOriginX(){ testSuite.addTest(testCase);
System.out.println("[testOriginX]"); TestRunner.run(testSuite);
assertEquals(0, orignPoint.getX()); }
System.out.println("[[testOriginX]]"); }
}
13. Examples (7/7)
• Execution Result (Case 3)
setUp will be invoked
before each test method
tearDown will be invoked
testSuite = new
before each tested method
TestSuite(OriginPointTest.class);
testCase = new
OriginPointTest("prefixIsNotTest_testAbnormal")
Failure means a test case fail
Error means a test case
occurs exception
16. Summary
• new TestCase("testMethod")
o run --> invoke the method "testMethod“
• new TestSuite(testCase.class)
o collects all the methods whose prefix is "test"
and add it as TestCase to the TestSuite.
o run --> invoke the method "run" in each TestCase.
• testSuite.addTest(...)
o testSuite.addTest(testCase)
o testSuite.addTest(testSuite2)