SlideShare a Scribd company logo
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

PPT
testng
harithakannan
 
KEY
Test-driven development for TYPO3 (T3DD11)
Oliver Klee
 
PPTX
Unit testing
NexThoughts Technologies
 
PDF
JUnit Pioneer
Scott Leberknight
 
KEY
Test-Driven Development for TYPO3
Oliver Klee
 
KEY
Test-Driven Development for TYPO3 @ T3CON12DE
Oliver Klee
 
PPTX
Test ng
Ramakrishna kapa
 
PPT
Junit and testNG
Марія Русин
 
Test-driven development for TYPO3 (T3DD11)
Oliver Klee
 
JUnit Pioneer
Scott Leberknight
 
Test-Driven Development for TYPO3
Oliver Klee
 
Test-Driven Development for TYPO3 @ T3CON12DE
Oliver Klee
 
Junit and testNG
Марія Русин
 

What's hot (20)

ODP
Intro to Testing in Zope, Plone
Quintagroup
 
PDF
Lab4
siragezeynu
 
PDF
Important java programs(collection+file)
Alok Kumar
 
PPT
2012 JDays Bad Tests Good Tests
Tomek Kaczanowski
 
PDF
TestNG - The Next Generation of Unit Testing
Bethmi Gunasekara
 
PDF
Java Programming - 03 java control flow
Danairat Thanabodithammachari
 
PDF
Java custom annotations example
Milton José Ferreira
 
PDF
TestNG introduction
Denis Bazhin
 
PPTX
Smarter Testing With Spock
IT Weekend
 
PDF
Server1
FahriIrawan3
 
PDF
DynaMine: Finding Common Error Patterns by Mining Software Revision Histories
Thomas Zimmermann
 
PDF
Spock framework
Djair Carvalho
 
PPTX
Pragmatic unittestingwithj unit
liminescence
 
PDF
33rd Degree 2013, Bad Tests, Good Tests
Tomek Kaczanowski
 
DOCX
201913046 wahyu septiansyah network programing
wahyuseptiansyah
 
PDF
Testing, Performance Analysis, and jQuery 1.4
jeresig
 
PPTX
Test NG Framework Complete Walk Through
Narendran Solai Sridharan
 
PDF
GMock framework
corehard_by
 
Intro to Testing in Zope, Plone
Quintagroup
 
Important java programs(collection+file)
Alok Kumar
 
2012 JDays Bad Tests Good Tests
Tomek Kaczanowski
 
TestNG - The Next Generation of Unit Testing
Bethmi Gunasekara
 
Java Programming - 03 java control flow
Danairat Thanabodithammachari
 
Java custom annotations example
Milton José Ferreira
 
TestNG introduction
Denis Bazhin
 
Smarter Testing With Spock
IT Weekend
 
Server1
FahriIrawan3
 
DynaMine: Finding Common Error Patterns by Mining Software Revision Histories
Thomas Zimmermann
 
Spock framework
Djair Carvalho
 
Pragmatic unittestingwithj unit
liminescence
 
33rd Degree 2013, Bad Tests, Good Tests
Tomek Kaczanowski
 
201913046 wahyu septiansyah network programing
wahyuseptiansyah
 
Testing, Performance Analysis, and jQuery 1.4
jeresig
 
Test NG Framework Complete Walk Through
Narendran Solai Sridharan
 
GMock framework
corehard_by
 
Ad

Similar to JUnit (20)

PPTX
Junit 4.0
pallavikhandekar212
 
PPTX
Unit Testing with JUnit4 by Ravikiran Janardhana
Ravikiran J
 
PPTX
Junit4&testng presentation
Sanjib Dhar
 
PPTX
Cpp unit
mudabbirwarsi
 
PDF
Software Testing - Invited Lecture at UNSW Sydney
julien.ponge
 
PDF
GeeCON 2012 Bad Tests, Good Tests
Tomek Kaczanowski
 
PDF
Confitura 2012 Bad Tests, Good Tests
Tomek Kaczanowski
 
PPT
3 j unit
kishoregali
 
PDF
Refactoring In Tdd The Missing Part
Gabriele Lana
 
PPS
JUnit Presentation
priya_trivedi
 
PPS
J unit presentation
Priya Sharma
 
DOCX
Junit With Eclipse
Sunil kumar Mohanty
 
PDF
Unit testing with JUnit
Thomas Zimmermann
 
PDF
P&MSP2012 - Unit Testing
Daniele Dell'Aglio
 
PPT
J Unit
guest333f37c3
 
KEY
How to Start Test-Driven Development in Legacy Code
Daniel Wellman
 
PPTX
Test driven development
John Walsh
 
PPTX
Unit Testing in Java
Ahmed M. Gomaa
 
PPTX
Introduction to JUnit
Devvrat Shukla
 
Unit Testing with JUnit4 by Ravikiran Janardhana
Ravikiran J
 
Junit4&testng presentation
Sanjib Dhar
 
Cpp unit
mudabbirwarsi
 
Software Testing - Invited Lecture at UNSW Sydney
julien.ponge
 
GeeCON 2012 Bad Tests, Good Tests
Tomek Kaczanowski
 
Confitura 2012 Bad Tests, Good Tests
Tomek Kaczanowski
 
3 j unit
kishoregali
 
Refactoring In Tdd The Missing Part
Gabriele Lana
 
JUnit Presentation
priya_trivedi
 
J unit presentation
Priya Sharma
 
Junit With Eclipse
Sunil kumar Mohanty
 
Unit testing with JUnit
Thomas Zimmermann
 
P&MSP2012 - Unit Testing
Daniele Dell'Aglio
 
How to Start Test-Driven Development in Legacy Code
Daniel Wellman
 
Test driven development
John Walsh
 
Unit Testing in Java
Ahmed M. Gomaa
 
Introduction to JUnit
Devvrat Shukla
 
Ad

More from Li-Wei Cheng (7)

PDF
Introduction to AndroidMock
Li-Wei Cheng
 
PDF
Unit testing in android
Li-Wei Cheng
 
PDF
Make Your SW Component Testable
Li-Wei Cheng
 
PDF
Test doubles
Li-Wei Cheng
 
PDF
What's software testing
Li-Wei Cheng
 
PPTX
OO design principle
Li-Wei Cheng
 
PPT
UML knowledge
Li-Wei Cheng
 
Introduction to AndroidMock
Li-Wei Cheng
 
Unit testing in android
Li-Wei Cheng
 
Make Your SW Component Testable
Li-Wei Cheng
 
Test doubles
Li-Wei Cheng
 
What's software testing
Li-Wei Cheng
 
OO design principle
Li-Wei Cheng
 
UML knowledge
Li-Wei Cheng
 

Recently uploaded (20)

PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
The Future of Artificial Intelligence (AI)
Mukul
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
Software Development Methodologies in 2025
KodekX
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 

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)