SlideShare a Scribd company logo
1 of 25
Unit Testing with JAVA


Dinuka Malalanayake
13-12-2011




“Any Unit Test is better than none”
Objectives


    Unit testing?

    About JUnit

    How to do the Unit testing with JAVA

    Best Practices

    Advantages

    Disadvantages

    Similar Products
“Any Unit Test is better than none”
Unit testing?


    A unit test is a procedure used to verify that a
    particular module of source code is working
    properly

                          Mouse, Keyboard, Monitor


    Company wants to achieve three related goals
    −     Faster time-to-market
“Any Unit Test is better than none”
     − Higher quality
     − More flexibility
About Junit


    Junit is an open source unit testing
    framework for JAVA.

    Junit has plugin for Eclipse, NetBeans.....etc.

    It serves the same purpose as NUnit does in
    the DotNet Technologies, and is one of many
    in the xUnit family


“Any Unit Test is better than none”
Making the Environment


    Download the eclipse

    Configure JUnit or Testng framework
How to do the Unit testing with
                  JAVA?

    Pre Requirement
     −   Source code
     −   Test Framework
     −   Test Methods




“Any Unit Test is better than none”
Test Fixture in Junit

    import org.junit.*;


    @Test

    @Before

    @After

    @BeforeClass

    @AfterClass

    @Ignore….etc.

“Any Unit Test is better than none”
Test Fixture in Testng


    import org.testng.*


    @Test

    @BeforeMethod

    @AfterMethod

    @BeforeClass

    @AfterClass

    @Test (enabled=false)….etc.
“Any Unit Test is better than none”
Way of working in Junit


    @BeforeClass
    −   @Before
         
           @Test
    −   @After


    −   @Before
         
           @Test
    −   @After

    @AfterClass
Way of working in Testng


    @BeforeClass
    −   @BeforeMethod
         
           @Test
    −   @AfterMethod


    −   @BeforeMethod
         
           @Test
    −   @AfterMethod

    @AfterClass
Assertion

    import junit.framework.Assert;


    assertEquals

    assertFalse

    assertNotNull

    assertNotSame

    assertNull

    fail…etc.

“Any Unit Test is better than none”
Steps for Unit testing

    Create JAVA Project by using the Eclipse

    Write the simple class
                 public class Account {

                 }

    Define the attributes and methods

    Create an Unit testing class

    Write down unit test code according to the
    specification

“Any Unit Test is better than none”
Class Account
Type          Name
float         minimumBalance
float         balance

Return type   Name
void          setMinimumBalanace(float minimumBalanace)
float         getMinimumBalanace()
float         deposit(float amount)
float         getBalance()
void          deposit(float amount)
void          withdraw(float amount)
void          calculateInterest(float interestRate)
void          reSetAccount()
Best Practices


    No conditional logic – Switch, if

    No loops

    Use appropriate method names

    Informative assertion message

    Separation per type



“Any Unit Test is better than none”
Advantages

    Fast

    Test Isolation

    Environment Isolation – Use Mock Objects

    Unit testing gives you a safety net when
    programmers re-factor or add functionality

    Unit tests can be used as documentation for
    other programmers

    Development process becomes more flexible

“Any Unit Test is better than none”
Disadvantages


    Unnecessary Unit Tests can lead to
    considerably high maintenance cost to your
    overall project




“Any Unit Test is better than none”
Similar Products
Programming Language                  Unit Testing Tool
C#                                    NUnit
Java – J2ME                           JMUnit
C                                     CUnit
C++                                   CppUnit
php                                   PHPUnit
Python                                PyUnit / py.test




“Any Unit Test is better than none”
Q&A




“Any Unit Test is better than none”
THANK YOU




“Any Unit Test is better than none”

More Related Content

What's hot

Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDDDror Helper
 
Integration testing
Integration testingIntegration testing
Integration testingVaibhav Dash
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testingAdam Stephensen
 
Pairwise testing - Strategic test case design
Pairwise testing - Strategic test case designPairwise testing - Strategic test case design
Pairwise testing - Strategic test case designXBOSoft
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPTsuhasreddy1
 
Automation Framework Presentation
Automation Framework PresentationAutomation Framework Presentation
Automation Framework PresentationBen Ngo
 
Automated Testing vs Manual Testing
Automated Testing vs Manual TestingAutomated Testing vs Manual Testing
Automated Testing vs Manual Testingdidev
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration TestingDavid Berliner
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practicesnickokiss
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit TestingJoe Tremblay
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And MockingJoe Wilson
 
Types of test tools
Types of test toolsTypes of test tools
Types of test toolsVaibhav Dash
 
ISTQB Foundation Level Basic
ISTQB Foundation Level BasicISTQB Foundation Level Basic
ISTQB Foundation Level BasicErol Selitektay
 
What is Web Testing?
What is Web Testing?   What is Web Testing?
What is Web Testing? QA InfoTech
 
Automation testing introduction for FujiNet
Automation testing introduction for FujiNetAutomation testing introduction for FujiNet
Automation testing introduction for FujiNetHai Tran Son
 
Automation - web testing with selenium
Automation - web testing with seleniumAutomation - web testing with selenium
Automation - web testing with seleniumTzirla Rozental
 

What's hot (20)

Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
Integration testing
Integration testingIntegration testing
Integration testing
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testing
 
Pairwise testing - Strategic test case design
Pairwise testing - Strategic test case designPairwise testing - Strategic test case design
Pairwise testing - Strategic test case design
 
Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPT
 
Unit testing
Unit testingUnit testing
Unit testing
 
Software testing
Software testingSoftware testing
Software testing
 
Automation Framework Presentation
Automation Framework PresentationAutomation Framework Presentation
Automation Framework Presentation
 
Automated Testing vs Manual Testing
Automated Testing vs Manual TestingAutomated Testing vs Manual Testing
Automated Testing vs Manual Testing
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration Testing
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practices
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 
Types of test tools
Types of test toolsTypes of test tools
Types of test tools
 
ISTQB Foundation Level Basic
ISTQB Foundation Level BasicISTQB Foundation Level Basic
ISTQB Foundation Level Basic
 
What is Web Testing?
What is Web Testing?   What is Web Testing?
What is Web Testing?
 
Automation testing introduction for FujiNet
Automation testing introduction for FujiNetAutomation testing introduction for FujiNet
Automation testing introduction for FujiNet
 
Junit
JunitJunit
Junit
 
Automation - web testing with selenium
Automation - web testing with seleniumAutomation - web testing with selenium
Automation - web testing with selenium
 

Similar to Unit testing with java

Similar to Unit testing with java (20)

Nunit
NunitNunit
Nunit
 
Junit
JunitJunit
Junit
 
Qt test framework
Qt test frameworkQt test framework
Qt test framework
 
Test Automation Principles
Test Automation PrinciplesTest Automation Principles
Test Automation Principles
 
8-testing.pptx
8-testing.pptx8-testing.pptx
8-testing.pptx
 
Good Practices On Test Automation
Good Practices On Test AutomationGood Practices On Test Automation
Good Practices On Test Automation
 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
 
Token Testing Slides
Token  Testing SlidesToken  Testing Slides
Token Testing Slides
 
Test ng tutorial
Test ng tutorialTest ng tutorial
Test ng tutorial
 
Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
DSR Testing (Part 1)
DSR Testing (Part 1)DSR Testing (Part 1)
DSR Testing (Part 1)
 
ikp321-04
ikp321-04ikp321-04
ikp321-04
 
Gallio Crafting A Toolchain
Gallio Crafting A ToolchainGallio Crafting A Toolchain
Gallio Crafting A Toolchain
 
Test ng for testers
Test ng for testersTest ng for testers
Test ng for testers
 
The Art of Unit Testing Feedback
The Art of Unit Testing FeedbackThe Art of Unit Testing Feedback
The Art of Unit Testing Feedback
 
Test ng
Test ngTest ng
Test ng
 
Intro to junit
Intro to junitIntro to junit
Intro to junit
 
An Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnitAn Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnit
 
Junit 4.0
Junit 4.0Junit 4.0
Junit 4.0
 

Recently uploaded

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 

Recently uploaded (20)

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 

Unit testing with java

  • 1. Unit Testing with JAVA Dinuka Malalanayake 13-12-2011 “Any Unit Test is better than none”
  • 2. Objectives  Unit testing?  About JUnit  How to do the Unit testing with JAVA  Best Practices  Advantages  Disadvantages  Similar Products “Any Unit Test is better than none”
  • 3. Unit testing?  A unit test is a procedure used to verify that a particular module of source code is working properly Mouse, Keyboard, Monitor  Company wants to achieve three related goals − Faster time-to-market “Any Unit Test is better than none” − Higher quality − More flexibility
  • 4. About Junit  Junit is an open source unit testing framework for JAVA.  Junit has plugin for Eclipse, NetBeans.....etc.  It serves the same purpose as NUnit does in the DotNet Technologies, and is one of many in the xUnit family “Any Unit Test is better than none”
  • 5. Making the Environment  Download the eclipse  Configure JUnit or Testng framework
  • 6.
  • 7.
  • 8.
  • 9.
  • 10. How to do the Unit testing with JAVA?  Pre Requirement − Source code − Test Framework − Test Methods “Any Unit Test is better than none”
  • 11. Test Fixture in Junit  import org.junit.*;  @Test  @Before  @After  @BeforeClass  @AfterClass  @Ignore….etc. “Any Unit Test is better than none”
  • 12. Test Fixture in Testng  import org.testng.*  @Test  @BeforeMethod  @AfterMethod  @BeforeClass  @AfterClass  @Test (enabled=false)….etc. “Any Unit Test is better than none”
  • 13. Way of working in Junit  @BeforeClass − @Before  @Test − @After − @Before  @Test − @After  @AfterClass
  • 14. Way of working in Testng  @BeforeClass − @BeforeMethod  @Test − @AfterMethod − @BeforeMethod  @Test − @AfterMethod  @AfterClass
  • 15. Assertion  import junit.framework.Assert;  assertEquals  assertFalse  assertNotNull  assertNotSame  assertNull  fail…etc. “Any Unit Test is better than none”
  • 16. Steps for Unit testing  Create JAVA Project by using the Eclipse  Write the simple class public class Account { }  Define the attributes and methods  Create an Unit testing class  Write down unit test code according to the specification “Any Unit Test is better than none”
  • 17. Class Account Type Name float minimumBalance float balance Return type Name void setMinimumBalanace(float minimumBalanace) float getMinimumBalanace() float deposit(float amount) float getBalance() void deposit(float amount) void withdraw(float amount) void calculateInterest(float interestRate) void reSetAccount()
  • 18.
  • 19.
  • 20. Best Practices  No conditional logic – Switch, if  No loops  Use appropriate method names  Informative assertion message  Separation per type “Any Unit Test is better than none”
  • 21. Advantages  Fast  Test Isolation  Environment Isolation – Use Mock Objects  Unit testing gives you a safety net when programmers re-factor or add functionality  Unit tests can be used as documentation for other programmers  Development process becomes more flexible “Any Unit Test is better than none”
  • 22. Disadvantages  Unnecessary Unit Tests can lead to considerably high maintenance cost to your overall project “Any Unit Test is better than none”
  • 23. Similar Products Programming Language Unit Testing Tool C# NUnit Java – J2ME JMUnit C CUnit C++ CppUnit php PHPUnit Python PyUnit / py.test “Any Unit Test is better than none”
  • 24. Q&A “Any Unit Test is better than none”
  • 25. THANK YOU “Any Unit Test is better than none”