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

                Dinuka Malalanayake
                13-12-2011



                “Any Unit Test is better than none”

© Cambio Healthcare Systems AB                        -1-   12-12-19
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”

© Cambio Healthcare Systems AB                         -2-   12-12-19
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
                – Higher quality
                – More flexibility
                “Any Unit Test is better than none”

© Cambio Healthcare Systems AB                           -3-                 12-12-19
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”

© Cambio Healthcare Systems AB                        -4-                 12-12-19
Making the Environment

             • Download the eclipse
             • Configure JUnit or Testng framework




© Cambio Healthcare Systems AB          -5-          12-12-19
© Cambio Healthcare Systems AB   -6-   12-12-19
© Cambio Healthcare Systems AB   -7-   12-12-19
© Cambio Healthcare Systems AB   -8-   12-12-19
© Cambio Healthcare Systems AB   -9-   12-12-19
How to do the Unit testing with JAVA?

             • Pre Requirement
                – Source code
                – Test Framework
                – Test Methods




                “Any Unit Test is better than none”

© Cambio Healthcare Systems AB                        -10-   12-12-19
Test Fixture in Junit

             • import org.junit.*;

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

                 “Any Unit Test is better than none”

© Cambio Healthcare Systems AB                         -11-   12-12-19
Test Fixture in Testng

             • import org.testng.*

             •     @Test
             •     @BeforeMethod
             •     @AfterMethod
             •     @BeforeClass
             •     @AfterClass
             •     @Test (enabled=false)….etc.

                 “Any Unit Test is better than none”

© Cambio Healthcare Systems AB                         -12-   12-12-19
Way of working in Junit

             • @BeforeClass
                      – @Before
                         • @Test
                      – @After

                      – @Before
                         • @Test
                      – @After
             • @AfterClass



© Cambio Healthcare Systems AB        -13-   12-12-19
Way of working in Testng

             • @BeforeClass
                      – @BeforeMethod
                         • @Test
                      – @AfterMethod

                      – @BeforeMethod
                         • @Test
                      – @AfterMethod
             • @AfterClass



© Cambio Healthcare Systems AB          -14-   12-12-19
Assertion

             • import junit.framework.Assert;

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

                 “Any Unit Test is better than none”

© Cambio Healthcare Systems AB                         -15-   12-12-19
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”

© Cambio Healthcare Systems AB                            -16-            12-12-19
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()




© Cambio Healthcare Systems AB                           -17-                12-12-19
© Cambio Healthcare Systems AB   -18-   12-12-19
© Cambio Healthcare Systems AB   -19-   12-12-19
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”

© Cambio Healthcare Systems AB                         -20-   12-12-19
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”

© Cambio Healthcare Systems AB                         -21-           12-12-19
Disadvantages

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




                “Any Unit Test is better than none”

© Cambio Healthcare Systems AB                        -22-            12-12-19
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”

© Cambio Healthcare Systems AB                        -23-                       12-12-19
Q&A




                “Any Unit Test is better than none”

© Cambio Healthcare Systems AB                        -24-   12-12-19
THANK YOU




                “Any Unit Test is better than none”

© Cambio Healthcare Systems AB                           -25-     12-12-19

More Related Content

What's hot

GeeCON 2012 hurdle run through ejb testing
GeeCON 2012 hurdle run through ejb testingGeeCON 2012 hurdle run through ejb testing
GeeCON 2012 hurdle run through ejb testing
Jakub Marchwicki
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practices
nickokiss
 

What's hot (20)

Software testing basics and its types
Software testing basics and its typesSoftware testing basics and its types
Software testing basics and its types
 
GeeCON 2012 hurdle run through ejb testing
GeeCON 2012 hurdle run through ejb testingGeeCON 2012 hurdle run through ejb testing
GeeCON 2012 hurdle run through ejb testing
 
Oh so you test? - A guide to testing on Android from Unit to Mutation
Oh so you test? - A guide to testing on Android from Unit to MutationOh so you test? - A guide to testing on Android from Unit to Mutation
Oh so you test? - A guide to testing on Android from Unit to Mutation
 
Unit Test in Ruby on Rails by Minitest
Unit Test in Ruby on Rails by MinitestUnit Test in Ruby on Rails by Minitest
Unit Test in Ruby on Rails by Minitest
 
Testing review-3
Testing review-3Testing review-3
Testing review-3
 
Unit testing
Unit testingUnit testing
Unit testing
 
2013 DevFest Vienna - Bad Tests, Good Tests
2013 DevFest Vienna - Bad Tests, Good Tests2013 DevFest Vienna - Bad Tests, Good Tests
2013 DevFest Vienna - Bad Tests, Good Tests
 
Dev labs alliance top 20 testng interview questions for sdet
Dev labs alliance top 20 testng interview questions for sdetDev labs alliance top 20 testng interview questions for sdet
Dev labs alliance top 20 testng interview questions for sdet
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practices
 
(Unit )-Testing for Joomla
(Unit )-Testing for Joomla(Unit )-Testing for Joomla
(Unit )-Testing for Joomla
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good Tests
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good Tests
 
Creating Maintainable Automated Acceptance Tests
Creating Maintainable Automated Acceptance TestsCreating Maintainable Automated Acceptance Tests
Creating Maintainable Automated Acceptance Tests
 
JUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkJUnit- A Unit Testing Framework
JUnit- A Unit Testing Framework
 
Test driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practicesTest driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practices
 
Unit testing best practices with JUnit
Unit testing best practices with JUnitUnit testing best practices with JUnit
Unit testing best practices with JUnit
 
Design For Testability
Design For TestabilityDesign For Testability
Design For Testability
 
Best practices unit testing
Best practices unit testing Best practices unit testing
Best practices unit testing
 
Combinatorial software test design beyond pairwise testing
Combinatorial software test design beyond pairwise testingCombinatorial software test design beyond pairwise testing
Combinatorial software test design beyond pairwise testing
 
Junit 4.0
Junit 4.0Junit 4.0
Junit 4.0
 

Viewers also liked

Viewers also liked (16)

Maximizando o valor e não a vazão das entregas
Maximizando o valor e não a vazão das entregasMaximizando o valor e não a vazão das entregas
Maximizando o valor e não a vazão das entregas
 
Đề thi viên chức chuyên ngành công nghiệp ( Trắc Nghiệm) De thi 3
Đề thi viên chức chuyên ngành công nghiệp ( Trắc Nghiệm) De thi 3Đề thi viên chức chuyên ngành công nghiệp ( Trắc Nghiệm) De thi 3
Đề thi viên chức chuyên ngành công nghiệp ( Trắc Nghiệm) De thi 3
 
Limpador leve
Limpador leve  Limpador leve
Limpador leve
 
Expansão do objeto de design
Expansão do objeto de designExpansão do objeto de design
Expansão do objeto de design
 
Mobile Backend as a Service
Mobile Backend as a ServiceMobile Backend as a Service
Mobile Backend as a Service
 
Farmacia
FarmaciaFarmacia
Farmacia
 
گمرک
گمرکگمرک
گمرک
 
O papel do analista de negócios no mundo ágil
O papel do analista de negócios no mundo ágilO papel do analista de negócios no mundo ágil
O papel do analista de negócios no mundo ágil
 
Importância da anamnese e do exame físico
Importância da anamnese e do exame físicoImportância da anamnese e do exame físico
Importância da anamnese e do exame físico
 
UFLEX LAUNCHES DIRECT UV OFFSET PRINTABLE POLYESTER FILM
UFLEX LAUNCHES DIRECT UV OFFSET PRINTABLE POLYESTER FILMUFLEX LAUNCHES DIRECT UV OFFSET PRINTABLE POLYESTER FILM
UFLEX LAUNCHES DIRECT UV OFFSET PRINTABLE POLYESTER FILM
 
Etnografia no Design com Teoria da Atividade
Etnografia no Design com Teoria da AtividadeEtnografia no Design com Teoria da Atividade
Etnografia no Design com Teoria da Atividade
 
Artik cloud deview 2016
Artik cloud   deview 2016Artik cloud   deview 2016
Artik cloud deview 2016
 
Feridas neoplásicas
Feridas neoplásicasFeridas neoplásicas
Feridas neoplásicas
 
Cloud Security and Risk Management
Cloud Security and Risk ManagementCloud Security and Risk Management
Cloud Security and Risk Management
 
Conhecendo e Criando novas Retrospectivas - TDC POA e AgileTrends Belém
Conhecendo e Criando novas Retrospectivas - TDC POA e AgileTrends BelémConhecendo e Criando novas Retrospectivas - TDC POA e AgileTrends Belém
Conhecendo e Criando novas Retrospectivas - TDC POA e AgileTrends Belém
 
(بروشور مركز الخبرة العالمية للتدريب والاستشارات (جيتك
(بروشور مركز الخبرة العالمية للتدريب والاستشارات (جيتك(بروشور مركز الخبرة العالمية للتدريب والاستشارات (جيتك
(بروشور مركز الخبرة العالمية للتدريب والاستشارات (جيتك
 

Similar to Unit testing with java

Presentation
PresentationPresentation
Presentation
SATYALOK
 
Benefits of System Accelerators
Benefits of System AcceleratorsBenefits of System Accelerators
Benefits of System Accelerators
Perficient
 

Similar to Unit testing with java (20)

Analysis & Design of Business Systems - 5
Analysis & Design of Business Systems - 5Analysis & Design of Business Systems - 5
Analysis & Design of Business Systems - 5
 
Continuous Performance Testing: The New Standard
Continuous Performance Testing: The New StandardContinuous Performance Testing: The New Standard
Continuous Performance Testing: The New Standard
 
39.Testing-Part3.ppt
39.Testing-Part3.ppt39.Testing-Part3.ppt
39.Testing-Part3.ppt
 
Perf Intro Ppt
Perf Intro PptPerf Intro Ppt
Perf Intro Ppt
 
Test Expo 2009 Site Confidence & Seriti Consulting Load Test Case Study
Test Expo 2009   Site Confidence & Seriti Consulting   Load Test Case StudyTest Expo 2009   Site Confidence & Seriti Consulting   Load Test Case Study
Test Expo 2009 Site Confidence & Seriti Consulting Load Test Case Study
 
Nunit
NunitNunit
Nunit
 
Unit Test (单元测试)
Unit Test (单元测试)Unit Test (单元测试)
Unit Test (单元测试)
 
6 Tools for Improving IT Operations in ICS Environments
6 Tools for Improving IT Operations in ICS Environments6 Tools for Improving IT Operations in ICS Environments
6 Tools for Improving IT Operations in ICS Environments
 
Reinventing Performance Testing, CMG imPACt 2016 slides
  Reinventing Performance Testing, CMG imPACt 2016 slides  Reinventing Performance Testing, CMG imPACt 2016 slides
Reinventing Performance Testing, CMG imPACt 2016 slides
 
Unit Testing Case Study for COJUG - 05.11.2010
Unit Testing Case Study for COJUG - 05.11.2010Unit Testing Case Study for COJUG - 05.11.2010
Unit Testing Case Study for COJUG - 05.11.2010
 
Software testing
Software testingSoftware testing
Software testing
 
perf_intro_ppt.pdf
perf_intro_ppt.pdfperf_intro_ppt.pdf
perf_intro_ppt.pdf
 
Presentation
PresentationPresentation
Presentation
 
What is Load, Stress and Endurance Testing?
What is Load, Stress and Endurance Testing?What is Load, Stress and Endurance Testing?
What is Load, Stress and Endurance Testing?
 
Load and Performance Testing in Production, featuring Scott Barber
Load and Performance Testing in Production, featuring Scott BarberLoad and Performance Testing in Production, featuring Scott Barber
Load and Performance Testing in Production, featuring Scott Barber
 
Ncerc rlmca202 adm m3 ssm
Ncerc rlmca202  adm m3 ssmNcerc rlmca202  adm m3 ssm
Ncerc rlmca202 adm m3 ssm
 
The DevOps Dance - Shift Left, Shift Right - Get It Right
The DevOps Dance - Shift Left, Shift Right - Get It RightThe DevOps Dance - Shift Left, Shift Right - Get It Right
The DevOps Dance - Shift Left, Shift Right - Get It Right
 
Benefits of System Accelerators
Benefits of System AcceleratorsBenefits of System Accelerators
Benefits of System Accelerators
 
Lecture (Software Testing).pptx
Lecture (Software Testing).pptxLecture (Software Testing).pptx
Lecture (Software Testing).pptx
 
How EVERFI Moved from No Automation to Continuous Test Generation in 9 Months
How EVERFI Moved from No Automation to Continuous Test Generation in 9 MonthsHow EVERFI Moved from No Automation to Continuous Test Generation in 9 Months
How EVERFI Moved from No Automation to Continuous Test Generation in 9 Months
 

Unit testing with java

  • 1. Unit Testing with JAVA Dinuka Malalanayake 13-12-2011 “Any Unit Test is better than none” © Cambio Healthcare Systems AB -1- 12-12-19
  • 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” © Cambio Healthcare Systems AB -2- 12-12-19
  • 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 – Higher quality – More flexibility “Any Unit Test is better than none” © Cambio Healthcare Systems AB -3- 12-12-19
  • 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” © Cambio Healthcare Systems AB -4- 12-12-19
  • 5. Making the Environment • Download the eclipse • Configure JUnit or Testng framework © Cambio Healthcare Systems AB -5- 12-12-19
  • 6. © Cambio Healthcare Systems AB -6- 12-12-19
  • 7. © Cambio Healthcare Systems AB -7- 12-12-19
  • 8. © Cambio Healthcare Systems AB -8- 12-12-19
  • 9. © Cambio Healthcare Systems AB -9- 12-12-19
  • 10. How to do the Unit testing with JAVA? • Pre Requirement – Source code – Test Framework – Test Methods “Any Unit Test is better than none” © Cambio Healthcare Systems AB -10- 12-12-19
  • 11. Test Fixture in Junit • import org.junit.*; • @Test • @Before • @After • @BeforeClass • @AfterClass • @Ignore….etc. “Any Unit Test is better than none” © Cambio Healthcare Systems AB -11- 12-12-19
  • 12. Test Fixture in Testng • import org.testng.* • @Test • @BeforeMethod • @AfterMethod • @BeforeClass • @AfterClass • @Test (enabled=false)….etc. “Any Unit Test is better than none” © Cambio Healthcare Systems AB -12- 12-12-19
  • 13. Way of working in Junit • @BeforeClass – @Before • @Test – @After – @Before • @Test – @After • @AfterClass © Cambio Healthcare Systems AB -13- 12-12-19
  • 14. Way of working in Testng • @BeforeClass – @BeforeMethod • @Test – @AfterMethod – @BeforeMethod • @Test – @AfterMethod • @AfterClass © Cambio Healthcare Systems AB -14- 12-12-19
  • 15. Assertion • import junit.framework.Assert; • assertEquals • assertFalse • assertNotNull • assertNotSame • assertNull • fail…etc. “Any Unit Test is better than none” © Cambio Healthcare Systems AB -15- 12-12-19
  • 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” © Cambio Healthcare Systems AB -16- 12-12-19
  • 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() © Cambio Healthcare Systems AB -17- 12-12-19
  • 18. © Cambio Healthcare Systems AB -18- 12-12-19
  • 19. © Cambio Healthcare Systems AB -19- 12-12-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” © Cambio Healthcare Systems AB -20- 12-12-19
  • 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” © Cambio Healthcare Systems AB -21- 12-12-19
  • 22. Disadvantages • Unnecessary Unit Tests can lead to considerably high maintenance cost to your overall project “Any Unit Test is better than none” © Cambio Healthcare Systems AB -22- 12-12-19
  • 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” © Cambio Healthcare Systems AB -23- 12-12-19
  • 24. Q&A “Any Unit Test is better than none” © Cambio Healthcare Systems AB -24- 12-12-19
  • 25. THANK YOU “Any Unit Test is better than none” © Cambio Healthcare Systems AB -25- 12-12-19