SlideShare a Scribd company logo
1 of 17
Download to read offline
JUnit


Li-Wei Cheng
Outline
• What's JUnit ?
• Hierarchy for JUnit
• Examples
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.
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 name's 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 class's 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 constructor


2. TestSuite
   o   Collects all the test methods in TestCase


3. 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.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]]");       }
  }
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 ?

More Related Content

What's hot

Intro to Testing in Zope, Plone
Intro to Testing in Zope, PloneIntro to Testing in Zope, Plone
Intro to Testing in Zope, PloneQuintagroup
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)Alok Kumar
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good TestsTomek Kaczanowski
 
TestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit TestingTestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit TestingBethmi Gunasekara
 
TestNG introduction
TestNG introductionTestNG introduction
TestNG introductionDenis Bazhin
 
Smarter Testing With Spock
Smarter Testing With SpockSmarter Testing With Spock
Smarter Testing With SpockIT Weekend
 
DynaMine: Finding Common Error Patterns by Mining Software Revision Histories
DynaMine: Finding Common Error Patterns by Mining Software Revision HistoriesDynaMine: Finding Common Error Patterns by Mining Software Revision Histories
DynaMine: Finding Common Error Patterns by Mining Software Revision HistoriesThomas Zimmermann
 
Pragmatic unittestingwithj unit
Pragmatic unittestingwithj unitPragmatic unittestingwithj unit
Pragmatic unittestingwithj unitliminescence
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good TestsTomek Kaczanowski
 
201913046 wahyu septiansyah network programing
201913046 wahyu septiansyah network programing201913046 wahyu septiansyah network programing
201913046 wahyu septiansyah network programingwahyuseptiansyah
 
Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4jeresig
 

What's hot (20)

Intro to Testing in Zope, Plone
Intro to Testing in Zope, PloneIntro to Testing in Zope, Plone
Intro to Testing in Zope, Plone
 
TestNG
TestNGTestNG
TestNG
 
Lab4
Lab4Lab4
Lab4
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
Junit
JunitJunit
Junit
 
TestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit TestingTestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit Testing
 
Java Programming - 03 java control flow
Java Programming - 03 java control flowJava Programming - 03 java control flow
Java Programming - 03 java control flow
 
Java custom annotations example
Java custom annotations exampleJava custom annotations example
Java custom annotations example
 
TestNG introduction
TestNG introductionTestNG introduction
TestNG introduction
 
Smarter Testing With Spock
Smarter Testing With SpockSmarter Testing With Spock
Smarter Testing With Spock
 
Server1
Server1Server1
Server1
 
DynaMine: Finding Common Error Patterns by Mining Software Revision Histories
DynaMine: Finding Common Error Patterns by Mining Software Revision HistoriesDynaMine: Finding Common Error Patterns by Mining Software Revision Histories
DynaMine: Finding Common Error Patterns by Mining Software Revision Histories
 
Spock framework
Spock frameworkSpock framework
Spock framework
 
Pragmatic unittestingwithj unit
Pragmatic unittestingwithj unitPragmatic unittestingwithj unit
Pragmatic unittestingwithj unit
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
 
201913046 wahyu septiansyah network programing
201913046 wahyu septiansyah network programing201913046 wahyu septiansyah network programing
201913046 wahyu septiansyah network programing
 
Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4
 
Test NG Framework Complete Walk Through
Test NG Framework Complete Walk ThroughTest NG Framework Complete Walk Through
Test NG Framework Complete Walk Through
 
GMock framework
GMock frameworkGMock framework
GMock framework
 

Similar to JUnit

J unit스터디슬라이드
J unit스터디슬라이드J unit스터디슬라이드
J unit스터디슬라이드ksain
 
Test-driven Development for TYPO3
Test-driven Development for TYPO3Test-driven Development for TYPO3
Test-driven Development for TYPO3Oliver Klee
 
Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testingjeresig
 
Test in action week 2
Test in action   week 2Test in action   week 2
Test in action week 2Yi-Huan Chan
 
Unit Testing with JUnit4 by Ravikiran Janardhana
Unit Testing with JUnit4 by Ravikiran JanardhanaUnit Testing with JUnit4 by Ravikiran Janardhana
Unit Testing with JUnit4 by Ravikiran JanardhanaRavikiran J
 
Be smart when testing your Akka code
Be smart when testing your Akka codeBe smart when testing your Akka code
Be smart when testing your Akka codeMykhailo Kotsur
 
Testing basics for developers
Testing basics for developersTesting basics for developers
Testing basics for developersAnton Udovychenko
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsTomek Kaczanowski
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsTomek Kaczanowski
 
JUnit 5 - The Next Generation of JUnit - Ted's Tool Time
JUnit 5 - The Next Generation of JUnit - Ted's Tool TimeJUnit 5 - The Next Generation of JUnit - Ted's Tool Time
JUnit 5 - The Next Generation of JUnit - Ted's Tool TimeTed Vinke
 

Similar to JUnit (20)

Test ng tutorial
Test ng tutorialTest ng tutorial
Test ng tutorial
 
Power mock
Power mockPower mock
Power mock
 
3 j unit
3 j unit3 j unit
3 j unit
 
Testing with Junit4
Testing with Junit4Testing with Junit4
Testing with Junit4
 
J unit스터디슬라이드
J unit스터디슬라이드J unit스터디슬라이드
J unit스터디슬라이드
 
Test-driven Development for TYPO3
Test-driven Development for TYPO3Test-driven Development for TYPO3
Test-driven Development for TYPO3
 
Junit
JunitJunit
Junit
 
Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testing
 
TestNG Framework
TestNG Framework TestNG Framework
TestNG Framework
 
Junit 4.0
Junit 4.0Junit 4.0
Junit 4.0
 
Junit 5 - Maior e melhor
Junit 5 - Maior e melhorJunit 5 - Maior e melhor
Junit 5 - Maior e melhor
 
Test in action week 2
Test in action   week 2Test in action   week 2
Test in action week 2
 
Unit Testing with JUnit4 by Ravikiran Janardhana
Unit Testing with JUnit4 by Ravikiran JanardhanaUnit Testing with JUnit4 by Ravikiran Janardhana
Unit Testing with JUnit4 by Ravikiran Janardhana
 
Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
 
Be smart when testing your Akka code
Be smart when testing your Akka codeBe smart when testing your Akka code
Be smart when testing your Akka code
 
Testing basics for developers
Testing basics for developersTesting basics for developers
Testing basics for developers
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good Tests
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good Tests
 
Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
 
JUnit 5 - The Next Generation of JUnit - Ted's Tool Time
JUnit 5 - The Next Generation of JUnit - Ted's Tool TimeJUnit 5 - The Next Generation of JUnit - Ted's Tool Time
JUnit 5 - The Next Generation of JUnit - Ted's Tool Time
 

More from Li-Wei Cheng

Introduction to AndroidMock
Introduction to AndroidMockIntroduction to AndroidMock
Introduction to AndroidMockLi-Wei Cheng
 
Unit testing in android
Unit testing in androidUnit testing in android
Unit testing in androidLi-Wei Cheng
 
Make Your SW Component Testable
Make Your SW Component TestableMake Your SW Component Testable
Make Your SW Component TestableLi-Wei Cheng
 
What's software testing
What's software testingWhat's software testing
What's software testingLi-Wei Cheng
 
OO design principle
OO design principleOO design principle
OO design principleLi-Wei Cheng
 

More from Li-Wei Cheng (7)

Introduction to AndroidMock
Introduction to AndroidMockIntroduction to AndroidMock
Introduction to AndroidMock
 
Unit testing in android
Unit testing in androidUnit testing in android
Unit testing in android
 
Make Your SW Component Testable
Make Your SW Component TestableMake Your SW Component Testable
Make Your SW Component Testable
 
Test doubles
Test doublesTest doubles
Test doubles
 
What's software testing
What's software testingWhat's software testing
What's software testing
 
OO design principle
OO design principleOO design principle
OO design principle
 
UML knowledge
UML knowledgeUML knowledge
UML knowledge
 

Recently uploaded

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 

Recently uploaded (20)

The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 

JUnit

  • 2. Outline • What's JUnit ? • Hierarchy for JUnit • Examples
  • 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]]"); } }
  • 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)