KEMBAR78
Unit testing with java | PPT
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

Unit testing with java

  • 1.
    Unit Testing withJAVA 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 HealthcareSystems AB -6- 12-12-19
  • 7.
    © Cambio HealthcareSystems AB -7- 12-12-19
  • 8.
    © Cambio HealthcareSystems AB -8- 12-12-19
  • 9.
    © Cambio HealthcareSystems AB -9- 12-12-19
  • 10.
    How to dothe 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 inJunit • 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 inTestng • 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 workingin Junit • @BeforeClass – @Before • @Test – @After – @Before • @Test – @After • @AfterClass © Cambio Healthcare Systems AB -13- 12-12-19
  • 14.
    Way of workingin 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 Unittesting • 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 HealthcareSystems AB -18- 12-12-19
  • 19.
    © Cambio HealthcareSystems 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