Upcoming SlideShare
Loading in...5







Total Views
Views on SlideShare
Embed Views



1 Embed 2

http://wiki.servercentralen.net 2



Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    JUnit JUnit Presentation Transcript

    • 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 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.
    • 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 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.
    • 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
    • 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)
    • 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
    • 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; } }
    • 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]]"); } }
    • Examples (5/7)• Execution Result (Case 1)
    • Examples (6/7)• Execution Result (Case 2)
    • 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
    • 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 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)
    • Any Question ?