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

An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and MockitoAn Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockitoshaunthomas999
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesDerek Smith
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDDDror Helper
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And MockingJoe Wilson
 
Writing and using Hamcrest Matchers
Writing and using Hamcrest MatchersWriting and using Hamcrest Matchers
Writing and using Hamcrest MatchersShai Yallin
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit TestingJoe Tremblay
 
What is JUnit? | Edureka
What is JUnit? | EdurekaWhat is JUnit? | Edureka
What is JUnit? | EdurekaEdureka!
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first stepsRenato Primavera
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testingikhwanhayat
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testingAdam Stephensen
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressionsLogan Chien
 
Mockito a simple, intuitive mocking framework
Mockito   a simple, intuitive mocking frameworkMockito   a simple, intuitive mocking framework
Mockito a simple, intuitive mocking frameworkPhat VU
 

What's hot (20)

An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and MockitoAn Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
Junit
JunitJunit
Junit
 
Workshop unit test
Workshop   unit testWorkshop   unit test
Workshop unit test
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 
JUnit 5
JUnit 5JUnit 5
JUnit 5
 
Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Writing and using Hamcrest Matchers
Writing and using Hamcrest MatchersWriting and using Hamcrest Matchers
Writing and using Hamcrest Matchers
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Using Mockito
Using MockitoUsing Mockito
Using Mockito
 
What is JUnit? | Edureka
What is JUnit? | EdurekaWhat is JUnit? | Edureka
What is JUnit? | Edureka
 
Junit
JunitJunit
Junit
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testing
 
Mockito
MockitoMockito
Mockito
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressions
 
Mockito a simple, intuitive mocking framework
Mockito   a simple, intuitive mocking frameworkMockito   a simple, intuitive mocking framework
Mockito a simple, intuitive mocking framework
 

Similar to Unit testing with java

Similar to Unit testing with java (20)

Nunit
NunitNunit
Nunit
 
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
 
Designing Top-Class Test Suites for Web Applications
Designing Top-Class Test Suites for Web ApplicationsDesigning Top-Class Test Suites for Web Applications
Designing Top-Class Test Suites for Web Applications
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
 

Recently uploaded

Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
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
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
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
 
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
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
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
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
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
 

Recently uploaded (20)

Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
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...
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
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
 
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
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
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
 
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
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
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
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
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 ...
 

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”