Your SlideShare is downloading. ×
  • Like
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply




Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads


Total Views
On SlideShare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. JUnitLi-Wei Cheng
  • 2. Outline• Whats JUnit ?• Hierarchy for JUnit• Examples
  • 3. Whats 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.
  • 4. Hierarchy for JUnit (1/2)
  • 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 names 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 classs method. (Test Class) (Tested Class)
  • 7. Examples (1/7)1. TestCase o A specific test method which is indicated when invoking the constructor2. TestSuite o Collects all the test methods in TestCase3. TestSuite (1+2) o Collects the Tests (TestCase + TestSuite)
  • 8. Examples (2/7)• Directory structure for example o tw/xxxx/shape/ o tw/xxxx/shape/tests xxxx location to junit• Compile & Run xxxx xxxx xxxx
  • 9. Examples (3/7)• 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.javapublic 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]]");; System.out.println("case 2"); }; System.out.println("case 3"); public void testOriginX(){ testSuite.addTest(testCase); System.out.println("[testOriginX]");; assertEquals(0, orignPoint.getX()); } System.out.println("[[testOriginX]]"); } }
  • 11. Examples (5/7)• Execution Result (Case 1)
  • 12. Examples (6/7)• Execution Result (Case 2)
  • 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
  • 14. Sequence Diagram (1/2)• A TestRunner run a TestCase.
  • 15. Sequence Diagram (2/2)• A TestRunner run a TestSuite. run it recursively
  • 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)
  • 17. Any Question ?