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 (20)

Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
 
JUnit 5
JUnit 5JUnit 5
JUnit 5
 
Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
 
N Unit Presentation
N Unit PresentationN Unit Presentation
N Unit Presentation
 
Automation testing & Unit testing
Automation testing & Unit testingAutomation testing & Unit testing
Automation testing & Unit testing
 
JUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkJUnit- A Unit Testing Framework
JUnit- A Unit Testing Framework
 
Mocking in Java with Mockito
Mocking in Java with MockitoMocking in Java with Mockito
Mocking in Java with Mockito
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introduction
 
TestNG
TestNGTestNG
TestNG
 
TestNG introduction
TestNG introductionTestNG introduction
TestNG introduction
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
 
TestNG with selenium
TestNG with seleniumTestNG with selenium
TestNG with selenium
 
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
 
Unit testing
Unit testingUnit testing
Unit testing
 
Nunit
NunitNunit
Nunit
 
Unit test-using-spock in Grails
Unit test-using-spock in GrailsUnit test-using-spock in Grails
Unit test-using-spock in Grails
 
Unit testing framework
Unit testing frameworkUnit testing framework
Unit testing framework
 
Test Automation
Test AutomationTest Automation
Test Automation
 
Apache jMeter
Apache jMeterApache jMeter
Apache jMeter
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript Basics
 

Similar to Unit testing with java

Qt test framework
Qt test frameworkQt test framework
Qt test frameworkICS
 
Test Automation Principles
Test Automation PrinciplesTest Automation Principles
Test Automation PrinciplesNetSuite
 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Javaguy_davis
 
Token Testing Slides
Token  Testing SlidesToken  Testing Slides
Token Testing Slidesericholscher
 
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_v2Tricode (part of Dept)
 
DSR Testing (Part 1)
DSR Testing (Part 1)DSR Testing (Part 1)
DSR Testing (Part 1)Steve Upton
 
The Art of Unit Testing Feedback
The Art of Unit Testing FeedbackThe Art of Unit Testing Feedback
The Art of Unit Testing FeedbackDeon Huang
 
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 ApplicationsLukáš Fryč
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development CodeOps Technologies LLP
 
Efficient JavaScript Unit Testing, March 2013
Efficient JavaScript Unit Testing, March 2013Efficient JavaScript Unit Testing, March 2013
Efficient JavaScript Unit Testing, March 2013Hazem Saleh
 

Similar to Unit testing with java (20)

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
 
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
 
Selenium TestNG
Selenium TestNGSelenium TestNG
Selenium TestNG
 
Efficient JavaScript Unit Testing, March 2013
Efficient JavaScript Unit Testing, March 2013Efficient JavaScript Unit Testing, March 2013
Efficient JavaScript Unit Testing, March 2013
 

Recently uploaded

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
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
 
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
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
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
 
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
 
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
 
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
 
#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
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
[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
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
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
 
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
 

Recently uploaded (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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
 
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
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
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...
 
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...
 
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...
 
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
 
#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
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
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...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
[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
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
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
 
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
 

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”