Your SlideShare is downloading. ×
0
JUnitLi-Wei Cheng
Outline• Whats JUnit ?• Hierarchy for JUnit• Examples
Whats JUnit•   A unit test framework for Java•   Originated from xUnit•   An important component in test-driven    develop...
Hierarchy for JUnit (1/2)
Hierarchy for JUnit (2/2)•   TestRunner    o   Use to run the test & get/show the test result•   Test    o   Define the in...
Something you must know before UT•   Test Class    o   The class you use to test the tested class.•   Tested Class    o   ...
Examples (1/7)1. TestCase   o   A specific test method which is indicated when       invoking the constructor2. TestSuite ...
Examples (2/7)•   Directory structure for example    o   tw/xxxx/shape/           Point.java           OriginPoint.java   ...
Examples (3/7)• Point.java  public class Point{    private int x;    private int y;      public Point(int x, int y){      ...
Examples (4/7)      •      OriginPointTest.javapublic class OriginPointTest extends TestCase{       public void testOrigin...
Examples (5/7)•   Execution Result (Case 1)
Examples (6/7)•   Execution Result (Case 2)
Examples (7/7)•   Execution Result (Case 3)                                                             setUp will be invo...
Sequence Diagram (1/2)•   A TestRunner run a TestCase.
Sequence Diagram (2/2)•   A TestRunner run a TestSuite.                         run it recursively
Summary•   new TestCase("testMethod")    o   run --> invoke the method "testMethod“•   new TestSuite(testCase.class)    o ...
Any Question ?
Upcoming SlideShare
Loading in...5
×

JUnit

385

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
385
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
23
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "JUnit"

  1. 1. JUnitLi-Wei Cheng
  2. 2. Outline• Whats JUnit ?• Hierarchy for JUnit• Examples
  3. 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. 4. Hierarchy for JUnit (1/2)
  5. 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. 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) Origin2DPointTest.java Origin2DPoint.java
  7. 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. 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. 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. 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]]"); 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]]"); } }
  11. 11. Examples (5/7)• Execution Result (Case 1)
  12. 12. Examples (6/7)• Execution Result (Case 2)
  13. 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. 14. Sequence Diagram (1/2)• A TestRunner run a TestCase.
  15. 15. Sequence Diagram (2/2)• A TestRunner run a TestSuite. run it recursively
  16. 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. 17. Any Question ?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×