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

Reflection in java
Reflection in javaReflection in java
Reflection in javaupen.rockin
 
Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Mohamed Taman
 
JUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkJUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkOnkar Deshpande
 
Testing in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkTesting in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkArulalan T
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in JavaSpotle.ai
 
Java Foundations: Lists, ArrayList<T>
Java Foundations: Lists, ArrayList<T>Java Foundations: Lists, ArrayList<T>
Java Foundations: Lists, ArrayList<T>Svetlin Nakov
 
Collections In Java
Collections In JavaCollections In Java
Collections In JavaBinoj T E
 
Lambda Expressions in Java | Java Lambda Tutorial | Java Certification Traini...
Lambda Expressions in Java | Java Lambda Tutorial | Java Certification Traini...Lambda Expressions in Java | Java Lambda Tutorial | Java Certification Traini...
Lambda Expressions in Java | Java Lambda Tutorial | Java Certification Traini...Edureka!
 
What is JUnit? | Edureka
What is JUnit? | EdurekaWhat is JUnit? | Edureka
What is JUnit? | EdurekaEdureka!
 
Refactoring and code smells
Refactoring and code smellsRefactoring and code smells
Refactoring and code smellsPaul Nguyen
 
Java Data Types
Java Data TypesJava Data Types
Java Data TypesSpotle.ai
 

What's hot (20)

Reflection in java
Reflection in javaReflection in java
Reflection in java
 
JUNit Presentation
JUNit PresentationJUNit Presentation
JUNit Presentation
 
Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.
 
JUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkJUnit- A Unit Testing Framework
JUnit- A Unit Testing Framework
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Java Annotations
Java AnnotationsJava Annotations
Java Annotations
 
Testing in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkTesting in-python-and-pytest-framework
Testing in-python-and-pytest-framework
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Java Foundations: Lists, ArrayList<T>
Java Foundations: Lists, ArrayList<T>Java Foundations: Lists, ArrayList<T>
Java Foundations: Lists, ArrayList<T>
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
 
testng
testngtestng
testng
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
Code Smells
Code SmellsCode Smells
Code Smells
 
Lambda Expressions in Java | Java Lambda Tutorial | Java Certification Traini...
Lambda Expressions in Java | Java Lambda Tutorial | Java Certification Traini...Lambda Expressions in Java | Java Lambda Tutorial | Java Certification Traini...
Lambda Expressions in Java | Java Lambda Tutorial | Java Certification Traini...
 
What is JUnit? | Edureka
What is JUnit? | EdurekaWhat is JUnit? | Edureka
What is JUnit? | Edureka
 
Refactoring and code smells
Refactoring and code smellsRefactoring and code smells
Refactoring and code smells
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
 
Fragment
Fragment Fragment
Fragment
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 

Similar to Unit testing with java

Similar to Unit testing with java (20)

Nunit
NunitNunit
Nunit
 
Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to 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

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 

Recently uploaded (20)

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 

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”