SlideShare a Scribd company logo
1 of 24
Download to read offline
Introduction to
UNIT TESTING & JUNIT
By:
DHEERAJ SHANDILYA
LinkedIn Id: http://in.linkedin.com/pub/dheeraj-shandilya/3a/77/5a2
Mindfire Solutions, India
U
n
i
t
T
e
s
t
i
n
g
Introduction to Unit Testing
What, why and how to do Unit Testing
Framework that will help
Introduction & History of Junit
Reference & Questions
We are going to discuss
Lets play with basic codes
Annotations and assertions in Junit
U
n
i
t
T
e
s
t
i
n
g
What is Unit Testing?
A Unit is the smallest possible part of application/system under test.
Testing is a process of checking/evaluating an attribute/characteristics
/capability of a program/object/method/application/..... and determine
that actual behavior meets with expected behavior.
Unit Testing is a process of writing test for specific part of code/system.
Unit testing help us reduce the number of bugs, time spent on debugging,
create a stable and reliable application.
U
n
i
t
T
e
s
t
i
n
g
What is Unit Testing?
A Unit is the smallest possible part of application/system under test.
Testing is a process of checking/evaluating an attribute/characteristics
/capability of a program/object/method/application/..... and determine
that actual behavior meets with expected behavior.
Unit Testing is a process of writing test for specific part of code/system.
Unit testing help us reduce the number of bugs, time spent on debugging,
create a stable and reliable application.
U
n
i
t
T
e
s
t
i
n
g
What is Unit Testing?
Static Analysis
Static unit testing offers the minimum path coverage
It helps in checking structural properties of code
Here we check syntax, unreachable codes,
undeclared/uninitialized variable, parameter type mismatch...
Dynamic Analysis
Dynamic unit testing detects control and data flow problems
It helps in checking the behavior of unit source code on test
data
Here we will check classes, methods, boundary condition,
logic errors...
U
n
i
t
T
e
s
t
i
n
g
1. If a unit is not working, it affects performance of complete system
For example
Consider any system say bike, car, mobile, human being anything.
Suppose mobile screen (lcd/led) is not working then you will not
be able to see menu over screen. Hence you can not work/operate.
Why to do Unit Testing?
U
n
i
t
T
e
s
t
i
n
g
2.There is strength in unity but not in unit.
It means with unit testing we can detect defect and fix bugs easily.
For example
Police interrogate suspects individually to get some more clues.
If different component/unit of a system works fine, then there are
More possibility that system (combining all component) will also
work fine
Why to do Unit Testing?
U
n
i
t
T
e
s
t
i
n
g
Why to do Unit Testing?
Easy to detect defect and debug
U
n
i
t
T
e
s
t
i
n
g
Benefits of Unit Testing?
> It Saves time on debugging. Hence effort and money too.
> It prepares the base for further testing like integration, functional
Testing...
> It enhances performance and provide obstacle free control.
> It ensures repeatability of desired behavior
> Unit Testing makes it easier to change and refactor code.
> Unit Testing inspires confidence.
> Unit Testing reduces the level of bugs in production code.
> We will get instant feedback
> It brings modularity in design
U
n
i
t
T
e
s
t
i
n
g
How to do Unit Testing
We have to write tests to check that the functionality of a unit,
often a class(or function/methods in the class) meet its
requirement.
Tests are written as the unit is developed.
So this testing is done by software engineer while development
The idea is that every time you make a change to the unit you
rerun the tests and see if your change has broken anything.
There are many tools and framework that will help us in unit
testing.
U
n
i
t
T
e
s
t
i
n
g
For different technology we can use different unit testing
framework. For example
Java: JUnit, TestNG...
C: Ctest, Cunit, Opmock ...
Python: Unittest, Py.test, Nose...
Visual Basic(VB6.0): vbunit, vbaunit, TinyUnit...
PHP: PHPunit, SnapTest ...
We can see complete list here
http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks
Tools & Framework
Introduction to Junit
JUnit: It is a framework for performing unit testing of Java programs.
It is used to write repeatable tests. For this we need Eclipse IDE. .
Eclipse contains a base workspace and an extensible plug-in system for
customizing the environment. It is used to develop application in various
languages...
JUnit is linked as a JAR at compile-time.
The framework resides under package junit.framework for JUnit 3.8 and
earlier, and under package org.junit for JUnit 4 and later.
History of Junit
1994
Sunit came
into existence
1997
Initiation
of Junit
2000
Junit Officially
Released
2002
Eclipse came
into existence
Annotations in Junit
@Test
public void method()
The @Test annotation identifies a method as a test method.
@Test (expected = Exception.class)
Fails, if the method does not throw the named exception.
@Test (timeout=time in millisec)
Fails, if the method/test timed out after specified time
@Ignore
Ignores the test method.
This is useful when the underlying code has been changed and the test case
has not yet been adapted. Or if the execution time of this test is too long to
be included.
Annotations in Junit
@Before
public void method()
This method is executed before each test. It is used to prepare the test environment
(e.g. read input data, initialize the class).
@After
public void method()
This method is executed after each test. It is used to cleanup the test environment
(e.g. delete temporary data, restore defaults). It can also save memory by cleaning
up expensive memory structures.
Annotations in Junit
Annotations in Junit
@BeforeClass
public static void method()
This method is executed once, before the start of all tests.
It is used to perform time intensive activities, for example to connect to a database.
Methods annotated with this annotation need to be defined as static to work with JUnit.
@AfterClass
public static void method()
This method is executed once, after all tests have been finished.
It is used to perform clean-up activities, for example to disconnect from a database.
Methods annotated with this annotation need to be defined as static to work with JUnit.
.
There are several Juint Assertion methods .
These are static methods defined in
org.junit.Assert class
An assertion is a statement of expected
outcome. In other words, it is the expected
result of your test.
Assert Method
Assert Method
Most common of them are
> Fail() - Fails test
> assertNotNull(0) /assertNull(0) - assert if object is (not) null
> assertFalse(b) /assertTrue(b) - asserts if b is false /true
> assertEquals(a, b) - Compares two objects using equals() method
> assertArrayEquals(a, b) - compares two arrays
Lets do some basic testing in Eclipse IDE
Best Practices In Unit Testing
Isolate test from other environment, classes, tests.
Self Descriptive
> Name of variable, classes, methods......
Avoid conditional logics(if else...), nested loops
Use various assertion method provided by framework.
> Assertion message should tell the problem. One can include business logics,
Separate the test based on type and business module
http://junit.sourceforge.net/javadoc/org/junit/Assert.html
Http://www.pluralsight.com
Http://www.youtube.com
Http://en.wikipedia.org/
Http://junit.org
Http://www.google.co.in
Credit goes to ...
Questions
? ?
Thank You

More Related Content

What's hot

JUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkJUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkOnkar Deshpande
 
JUnit 5 - The Next Generation of JUnit - Ted's Tool Time
JUnit 5 - The Next Generation of JUnit - Ted's Tool TimeJUnit 5 - The Next Generation of JUnit - Ted's Tool Time
JUnit 5 - The Next Generation of JUnit - Ted's Tool TimeTed Vinke
 
Advanced junit and mockito
Advanced junit and mockitoAdvanced junit and mockito
Advanced junit and mockitoMathieu Carbou
 
Simple Unit Testing With Netbeans 6.1
Simple Unit Testing With Netbeans 6.1Simple Unit Testing With Netbeans 6.1
Simple Unit Testing With Netbeans 6.1Kiki Ahmadi
 
Unit testing with Junit
Unit testing with JunitUnit testing with Junit
Unit testing with JunitValerio Maggio
 
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 practicesNarendra Pathai
 
Unit Testing with JUnit4 by Ravikiran Janardhana
Unit Testing with JUnit4 by Ravikiran JanardhanaUnit Testing with JUnit4 by Ravikiran Janardhana
Unit Testing with JUnit4 by Ravikiran JanardhanaRavikiran J
 
05 junit
05 junit05 junit
05 junitmha4
 
An Introduction To Unit Testing and TDD
An Introduction To Unit Testing and TDDAn Introduction To Unit Testing and TDD
An Introduction To Unit Testing and TDDAhmed Ehab AbdulAziz
 

What's hot (20)

JUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkJUnit- A Unit Testing Framework
JUnit- A Unit Testing Framework
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
JUnit 5 - The Next Generation of JUnit - Ted's Tool Time
JUnit 5 - The Next Generation of JUnit - Ted's Tool TimeJUnit 5 - The Next Generation of JUnit - Ted's Tool Time
JUnit 5 - The Next Generation of JUnit - Ted's Tool Time
 
Junit 4.0
Junit 4.0Junit 4.0
Junit 4.0
 
Advanced junit and mockito
Advanced junit and mockitoAdvanced junit and mockito
Advanced junit and mockito
 
Simple Unit Testing With Netbeans 6.1
Simple Unit Testing With Netbeans 6.1Simple Unit Testing With Netbeans 6.1
Simple Unit Testing With Netbeans 6.1
 
Testing In Java
Testing In JavaTesting In Java
Testing In Java
 
J Unit
J UnitJ Unit
J Unit
 
Unit testing with Junit
Unit testing with JunitUnit testing with Junit
Unit testing with Junit
 
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
 
Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
 
Unit Testing with JUnit4 by Ravikiran Janardhana
Unit Testing with JUnit4 by Ravikiran JanardhanaUnit Testing with JUnit4 by Ravikiran Janardhana
Unit Testing with JUnit4 by Ravikiran Janardhana
 
05 junit
05 junit05 junit
05 junit
 
JUnit 4
JUnit 4JUnit 4
JUnit 4
 
Testing with Junit4
Testing with Junit4Testing with Junit4
Testing with Junit4
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
 
Junit
JunitJunit
Junit
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
JUnit 5
JUnit 5JUnit 5
JUnit 5
 
An Introduction To Unit Testing and TDD
An Introduction To Unit Testing and TDDAn Introduction To Unit Testing and TDD
An Introduction To Unit Testing and TDD
 

Viewers also liked

Django mongodb -djangoday_
Django mongodb -djangoday_Django mongodb -djangoday_
Django mongodb -djangoday_WEBdeBS
 
Overview of Testing Talks at Pycon
Overview of Testing Talks at PyconOverview of Testing Talks at Pycon
Overview of Testing Talks at PyconJacqueline Kazil
 
Super Advanced Python –act1
Super Advanced Python –act1Super Advanced Python –act1
Super Advanced Python –act1Ke Wei Louis
 
Django e il Rap Elia Contini
Django e il Rap Elia ContiniDjango e il Rap Elia Contini
Django e il Rap Elia ContiniWEBdeBS
 
NoSql Day - Chiusura
NoSql Day - ChiusuraNoSql Day - Chiusura
NoSql Day - ChiusuraWEBdeBS
 
2016 py con2016_lightingtalk_php to python
2016 py con2016_lightingtalk_php to python2016 py con2016_lightingtalk_php to python
2016 py con2016_lightingtalk_php to pythonJiho Lee
 
Authentication & Authorization in ASPdotNet MVC
Authentication & Authorization in ASPdotNet MVCAuthentication & Authorization in ASPdotNet MVC
Authentication & Authorization in ASPdotNet MVCMindfire Solutions
 
Django - The Web framework for perfectionists with deadlines
Django - The Web framework for perfectionists with deadlinesDjango - The Web framework for perfectionists with deadlines
Django - The Web framework for perfectionists with deadlinesMarkus Zapke-Gründemann
 
2007 - 应用系统脆弱性概论
2007 - 应用系统脆弱性概论 2007 - 应用系统脆弱性概论
2007 - 应用系统脆弱性概论 Na Lee
 

Viewers also liked (20)

JUnit Sample
JUnit SampleJUnit Sample
JUnit Sample
 
JUnit PowerUp
JUnit PowerUpJUnit PowerUp
JUnit PowerUp
 
Junit
JunitJunit
Junit
 
Test Driven Development and JUnit
Test Driven Development and JUnitTest Driven Development and JUnit
Test Driven Development and JUnit
 
Vim for Mere Mortals
Vim for Mere MortalsVim for Mere Mortals
Vim for Mere Mortals
 
Website optimization
Website optimizationWebsite optimization
Website optimization
 
Django mongodb -djangoday_
Django mongodb -djangoday_Django mongodb -djangoday_
Django mongodb -djangoday_
 
Overview of Testing Talks at Pycon
Overview of Testing Talks at PyconOverview of Testing Talks at Pycon
Overview of Testing Talks at Pycon
 
Super Advanced Python –act1
Super Advanced Python –act1Super Advanced Python –act1
Super Advanced Python –act1
 
Django e il Rap Elia Contini
Django e il Rap Elia ContiniDjango e il Rap Elia Contini
Django e il Rap Elia Contini
 
NoSql Day - Chiusura
NoSql Day - ChiusuraNoSql Day - Chiusura
NoSql Day - Chiusura
 
2016 py con2016_lightingtalk_php to python
2016 py con2016_lightingtalk_php to python2016 py con2016_lightingtalk_php to python
2016 py con2016_lightingtalk_php to python
 
Authentication & Authorization in ASPdotNet MVC
Authentication & Authorization in ASPdotNet MVCAuthentication & Authorization in ASPdotNet MVC
Authentication & Authorization in ASPdotNet MVC
 
Digesting jQuery
Digesting jQueryDigesting jQuery
Digesting jQuery
 
Django - The Web framework for perfectionists with deadlines
Django - The Web framework for perfectionists with deadlinesDjango - The Web framework for perfectionists with deadlines
Django - The Web framework for perfectionists with deadlines
 
2007 - 应用系统脆弱性概论
2007 - 应用系统脆弱性概论 2007 - 应用系统脆弱性概论
2007 - 应用系统脆弱性概论
 
Load testing
Load testingLoad testing
Load testing
 
Html5 History-API
Html5 History-APIHtml5 History-API
Html5 History-API
 
Django-Queryset
Django-QuerysetDjango-Queryset
Django-Queryset
 
2 × 3 = 6
2 × 3 = 62 × 3 = 6
2 × 3 = 6
 

Similar to Introduction To UnitTesting & JUnit

Junit Interview Questions-ppt
Junit Interview Questions-pptJunit Interview Questions-ppt
Junit Interview Questions-pptMayank Kumar
 
JUnit with_mocking
JUnit with_mockingJUnit with_mocking
JUnit with_mockingZeeshan Khan
 
Top 20 Junit interview questions for sdet
Top 20 Junit interview questions for sdetTop 20 Junit interview questions for sdet
Top 20 Junit interview questions for sdetDevLabs Alliance
 
5 Best Unit Test Frameworks to Automate Unit Tests
5 Best Unit Test Frameworks to Automate Unit Tests5 Best Unit Test Frameworks to Automate Unit Tests
5 Best Unit Test Frameworks to Automate Unit TestsSerena Gray
 
Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Hong Le Van
 
Unit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptxUnit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptxKnoldus Inc.
 
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)
 
Unit & integration testing
Unit & integration testingUnit & integration testing
Unit & integration testingPavlo Hodysh
 
Project Onion unit test environment
Project Onion unit test environmentProject Onion unit test environment
Project Onion unit test environmentAbhinav Jha
 
Unit testing and junit
Unit testing and junitUnit testing and junit
Unit testing and junitÖmer Taşkın
 
J unit presentation
J unit presentationJ unit presentation
J unit presentationPriya Sharma
 
Open Source tools in Continuous Integration environment (case study for agil...
Open Source tools in Continuous Integration environment  (case study for agil...Open Source tools in Continuous Integration environment  (case study for agil...
Open Source tools in Continuous Integration environment (case study for agil...suwalki24.pl
 
Continuous Integration testing based on Selenium and Hudson
Continuous Integration testing based on Selenium and HudsonContinuous Integration testing based on Selenium and Hudson
Continuous Integration testing based on Selenium and HudsonZbyszek Mockun
 
Model Based System Random Test For Smart OS
Model Based System Random Test For Smart OSModel Based System Random Test For Smart OS
Model Based System Random Test For Smart OSLex Yu
 
Introduction to Unit Testing for Mule Flows using Munit(Java) - Part 1
Introduction to Unit Testing for Mule Flows using Munit(Java) - Part 1Introduction to Unit Testing for Mule Flows using Munit(Java) - Part 1
Introduction to Unit Testing for Mule Flows using Munit(Java) - Part 1Alex Fernandez
 
Automation testing & Unit testing
Automation testing & Unit testingAutomation testing & Unit testing
Automation testing & Unit testingKapil Rajpurohit
 

Similar to Introduction To UnitTesting & JUnit (20)

Junit Interview Questions-ppt
Junit Interview Questions-pptJunit Interview Questions-ppt
Junit Interview Questions-ppt
 
JUnit with_mocking
JUnit with_mockingJUnit with_mocking
JUnit with_mocking
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 
Top 20 Junit interview questions for sdet
Top 20 Junit interview questions for sdetTop 20 Junit interview questions for sdet
Top 20 Junit interview questions for sdet
 
Android testing part i
Android testing part iAndroid testing part i
Android testing part i
 
5 Best Unit Test Frameworks to Automate Unit Tests
5 Best Unit Test Frameworks to Automate Unit Tests5 Best Unit Test Frameworks to Automate Unit Tests
5 Best Unit Test Frameworks to Automate Unit Tests
 
Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++
 
Unit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptxUnit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptx
 
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
 
Unit & integration testing
Unit & integration testingUnit & integration testing
Unit & integration testing
 
Project Onion unit test environment
Project Onion unit test environmentProject Onion unit test environment
Project Onion unit test environment
 
Unit testing and junit
Unit testing and junitUnit testing and junit
Unit testing and junit
 
Why test with flex unit
Why test with flex unitWhy test with flex unit
Why test with flex unit
 
J unit presentation
J unit presentationJ unit presentation
J unit presentation
 
Open Source tools in Continuous Integration environment (case study for agil...
Open Source tools in Continuous Integration environment  (case study for agil...Open Source tools in Continuous Integration environment  (case study for agil...
Open Source tools in Continuous Integration environment (case study for agil...
 
Continuous Integration testing based on Selenium and Hudson
Continuous Integration testing based on Selenium and HudsonContinuous Integration testing based on Selenium and Hudson
Continuous Integration testing based on Selenium and Hudson
 
Model Based System Random Test For Smart OS
Model Based System Random Test For Smart OSModel Based System Random Test For Smart OS
Model Based System Random Test For Smart OS
 
Unit testing, principles
Unit testing, principlesUnit testing, principles
Unit testing, principles
 
Introduction to Unit Testing for Mule Flows using Munit(Java) - Part 1
Introduction to Unit Testing for Mule Flows using Munit(Java) - Part 1Introduction to Unit Testing for Mule Flows using Munit(Java) - Part 1
Introduction to Unit Testing for Mule Flows using Munit(Java) - Part 1
 
Automation testing & Unit testing
Automation testing & Unit testingAutomation testing & Unit testing
Automation testing & Unit testing
 

More from Mindfire Solutions (20)

Physician Search and Review
Physician Search and ReviewPhysician Search and Review
Physician Search and Review
 
diet management app
diet management appdiet management app
diet management app
 
Business Technology Solution
Business Technology SolutionBusiness Technology Solution
Business Technology Solution
 
Remote Health Monitoring
Remote Health MonitoringRemote Health Monitoring
Remote Health Monitoring
 
Influencer Marketing Solution
Influencer Marketing SolutionInfluencer Marketing Solution
Influencer Marketing Solution
 
ELMAH
ELMAHELMAH
ELMAH
 
High Availability of Azure Applications
High Availability of Azure ApplicationsHigh Availability of Azure Applications
High Availability of Azure Applications
 
IOT Hands On
IOT Hands OnIOT Hands On
IOT Hands On
 
Glimpse of Loops Vs Set
Glimpse of Loops Vs SetGlimpse of Loops Vs Set
Glimpse of Loops Vs Set
 
Oracle Sql Developer-Getting Started
Oracle Sql Developer-Getting StartedOracle Sql Developer-Getting Started
Oracle Sql Developer-Getting Started
 
Adaptive Layout In iOS 8
Adaptive Layout In iOS 8Adaptive Layout In iOS 8
Adaptive Layout In iOS 8
 
Introduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/MacIntroduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/Mac
 
LINQPad - utility Tool
LINQPad - utility ToolLINQPad - utility Tool
LINQPad - utility Tool
 
Get started with watch kit development
Get started with watch kit developmentGet started with watch kit development
Get started with watch kit development
 
Swift vs Objective-C
Swift vs Objective-CSwift vs Objective-C
Swift vs Objective-C
 
Material Design in Android
Material Design in AndroidMaterial Design in Android
Material Design in Android
 
Introduction to OData
Introduction to ODataIntroduction to OData
Introduction to OData
 
Ext js Part 2- MVC
Ext js Part 2- MVCExt js Part 2- MVC
Ext js Part 2- MVC
 
ExtJs Basic Part-1
ExtJs Basic Part-1ExtJs Basic Part-1
ExtJs Basic Part-1
 
Spring Security Introduction
Spring Security IntroductionSpring Security Introduction
Spring Security Introduction
 

Recently uploaded

Postgres indexes: how to make them work for your application
Postgres indexes: how to make them work for your applicationPostgres indexes: how to make them work for your application
Postgres indexes: how to make them work for your applicationBartosz Sypytkowski
 
Preparing BitVisor for Supporting Multiple Architectures
Preparing BitVisor for Supporting Multiple ArchitecturesPreparing BitVisor for Supporting Multiple Architectures
Preparing BitVisor for Supporting Multiple ArchitecturesAke Koomsin
 
ManageIQ - Sprint 234 Review - Slide Deck
ManageIQ - Sprint 234 Review - Slide DeckManageIQ - Sprint 234 Review - Slide Deck
ManageIQ - Sprint 234 Review - Slide DeckManageIQ
 
Agile is RED and BLUE. What can Software Developers learn from the evolution ...
Agile is RED and BLUE. What can Software Developers learn from the evolution ...Agile is RED and BLUE. What can Software Developers learn from the evolution ...
Agile is RED and BLUE. What can Software Developers learn from the evolution ...Fernando Barrancos
 
kawika Technologies Software Development company
kawika Technologies Software Development  companykawika Technologies Software Development  company
kawika Technologies Software Development companyKawika Technologies
 
Best Chatbot Development Company In India.pdf
Best Chatbot Development Company In India.pdfBest Chatbot Development Company In India.pdf
Best Chatbot Development Company In India.pdfMeon TECHNOLOGIES
 
What is Mendix and the concept of low-code development.docx
What is Mendix and the concept of low-code development.docxWhat is Mendix and the concept of low-code development.docx
What is Mendix and the concept of low-code development.docxTechnogeeks
 
Folding Cheat Sheet #3 - third in a series
Folding Cheat Sheet #3 - third in a seriesFolding Cheat Sheet #3 - third in a series
Folding Cheat Sheet #3 - third in a seriesPhilip Schwarz
 
Ptidej Architecture, Design, and Implementation in Action v2.1
Ptidej Architecture, Design, and Implementation in Action v2.1Ptidej Architecture, Design, and Implementation in Action v2.1
Ptidej Architecture, Design, and Implementation in Action v2.1Yann-Gaël Guéhéneuc
 
full course of software engineering mid term.pdf
full course of software engineering mid term.pdffull course of software engineering mid term.pdf
full course of software engineering mid term.pdfAbdul salam
 
How Muvi's Encoding Is Better Than The Rest
How Muvi's Encoding Is Better Than The RestHow Muvi's Encoding Is Better Than The Rest
How Muvi's Encoding Is Better Than The RestRoshan Dwivedi
 
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...OnePlan Solutions
 
Explore the Three Main Types of Logistics - Inbound Logistics, Outbound Logis...
Explore the Three Main Types of Logistics - Inbound Logistics, Outbound Logis...Explore the Three Main Types of Logistics - Inbound Logistics, Outbound Logis...
Explore the Three Main Types of Logistics - Inbound Logistics, Outbound Logis...Piyovi
 
SPSS Statistics - Encrypting a Syntax File in IBM SPSS Statistics.pptx
SPSS Statistics - Encrypting a Syntax File in IBM SPSS Statistics.pptxSPSS Statistics - Encrypting a Syntax File in IBM SPSS Statistics.pptx
SPSS Statistics - Encrypting a Syntax File in IBM SPSS Statistics.pptxVersion 1 Analytics
 
Front-End Fun: How to finally work with designers!
Front-End Fun: How to finally work with designers!Front-End Fun: How to finally work with designers!
Front-End Fun: How to finally work with designers!helenalozano6
 
Microsoft-Entra-Identity-and-Access-presentation.pdf
Microsoft-Entra-Identity-and-Access-presentation.pdfMicrosoft-Entra-Identity-and-Access-presentation.pdf
Microsoft-Entra-Identity-and-Access-presentation.pdfJohnDoe583546
 
Introduction to AI in the Retail Industry
Introduction to AI in the Retail IndustryIntroduction to AI in the Retail Industry
Introduction to AI in the Retail IndustryGet Genetica
 
Industry-Experienced Instructors for DevOps Training at NareshIT
Industry-Experienced Instructors for DevOps Training at NareshITIndustry-Experienced Instructors for DevOps Training at NareshIT
Industry-Experienced Instructors for DevOps Training at NareshITmanoharjgpsolutions
 
logical backup of Oracle Datapump-detailed.pptx
logical backup of Oracle Datapump-detailed.pptxlogical backup of Oracle Datapump-detailed.pptx
logical backup of Oracle Datapump-detailed.pptxRemote DBA Services
 

Recently uploaded (20)

Postgres indexes: how to make them work for your application
Postgres indexes: how to make them work for your applicationPostgres indexes: how to make them work for your application
Postgres indexes: how to make them work for your application
 
Preparing BitVisor for Supporting Multiple Architectures
Preparing BitVisor for Supporting Multiple ArchitecturesPreparing BitVisor for Supporting Multiple Architectures
Preparing BitVisor for Supporting Multiple Architectures
 
ManageIQ - Sprint 234 Review - Slide Deck
ManageIQ - Sprint 234 Review - Slide DeckManageIQ - Sprint 234 Review - Slide Deck
ManageIQ - Sprint 234 Review - Slide Deck
 
Agile is RED and BLUE. What can Software Developers learn from the evolution ...
Agile is RED and BLUE. What can Software Developers learn from the evolution ...Agile is RED and BLUE. What can Software Developers learn from the evolution ...
Agile is RED and BLUE. What can Software Developers learn from the evolution ...
 
Travel APIs
Travel                              APIsTravel                              APIs
Travel APIs
 
kawika Technologies Software Development company
kawika Technologies Software Development  companykawika Technologies Software Development  company
kawika Technologies Software Development company
 
Best Chatbot Development Company In India.pdf
Best Chatbot Development Company In India.pdfBest Chatbot Development Company In India.pdf
Best Chatbot Development Company In India.pdf
 
What is Mendix and the concept of low-code development.docx
What is Mendix and the concept of low-code development.docxWhat is Mendix and the concept of low-code development.docx
What is Mendix and the concept of low-code development.docx
 
Folding Cheat Sheet #3 - third in a series
Folding Cheat Sheet #3 - third in a seriesFolding Cheat Sheet #3 - third in a series
Folding Cheat Sheet #3 - third in a series
 
Ptidej Architecture, Design, and Implementation in Action v2.1
Ptidej Architecture, Design, and Implementation in Action v2.1Ptidej Architecture, Design, and Implementation in Action v2.1
Ptidej Architecture, Design, and Implementation in Action v2.1
 
full course of software engineering mid term.pdf
full course of software engineering mid term.pdffull course of software engineering mid term.pdf
full course of software engineering mid term.pdf
 
How Muvi's Encoding Is Better Than The Rest
How Muvi's Encoding Is Better Than The RestHow Muvi's Encoding Is Better Than The Rest
How Muvi's Encoding Is Better Than The Rest
 
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...
 
Explore the Three Main Types of Logistics - Inbound Logistics, Outbound Logis...
Explore the Three Main Types of Logistics - Inbound Logistics, Outbound Logis...Explore the Three Main Types of Logistics - Inbound Logistics, Outbound Logis...
Explore the Three Main Types of Logistics - Inbound Logistics, Outbound Logis...
 
SPSS Statistics - Encrypting a Syntax File in IBM SPSS Statistics.pptx
SPSS Statistics - Encrypting a Syntax File in IBM SPSS Statistics.pptxSPSS Statistics - Encrypting a Syntax File in IBM SPSS Statistics.pptx
SPSS Statistics - Encrypting a Syntax File in IBM SPSS Statistics.pptx
 
Front-End Fun: How to finally work with designers!
Front-End Fun: How to finally work with designers!Front-End Fun: How to finally work with designers!
Front-End Fun: How to finally work with designers!
 
Microsoft-Entra-Identity-and-Access-presentation.pdf
Microsoft-Entra-Identity-and-Access-presentation.pdfMicrosoft-Entra-Identity-and-Access-presentation.pdf
Microsoft-Entra-Identity-and-Access-presentation.pdf
 
Introduction to AI in the Retail Industry
Introduction to AI in the Retail IndustryIntroduction to AI in the Retail Industry
Introduction to AI in the Retail Industry
 
Industry-Experienced Instructors for DevOps Training at NareshIT
Industry-Experienced Instructors for DevOps Training at NareshITIndustry-Experienced Instructors for DevOps Training at NareshIT
Industry-Experienced Instructors for DevOps Training at NareshIT
 
logical backup of Oracle Datapump-detailed.pptx
logical backup of Oracle Datapump-detailed.pptxlogical backup of Oracle Datapump-detailed.pptx
logical backup of Oracle Datapump-detailed.pptx
 

Introduction To UnitTesting & JUnit

  • 1. Introduction to UNIT TESTING & JUNIT By: DHEERAJ SHANDILYA LinkedIn Id: http://in.linkedin.com/pub/dheeraj-shandilya/3a/77/5a2 Mindfire Solutions, India
  • 2. U n i t T e s t i n g Introduction to Unit Testing What, why and how to do Unit Testing Framework that will help Introduction & History of Junit Reference & Questions We are going to discuss Lets play with basic codes Annotations and assertions in Junit
  • 3. U n i t T e s t i n g What is Unit Testing? A Unit is the smallest possible part of application/system under test. Testing is a process of checking/evaluating an attribute/characteristics /capability of a program/object/method/application/..... and determine that actual behavior meets with expected behavior. Unit Testing is a process of writing test for specific part of code/system. Unit testing help us reduce the number of bugs, time spent on debugging, create a stable and reliable application.
  • 4. U n i t T e s t i n g What is Unit Testing? A Unit is the smallest possible part of application/system under test. Testing is a process of checking/evaluating an attribute/characteristics /capability of a program/object/method/application/..... and determine that actual behavior meets with expected behavior. Unit Testing is a process of writing test for specific part of code/system. Unit testing help us reduce the number of bugs, time spent on debugging, create a stable and reliable application.
  • 5. U n i t T e s t i n g What is Unit Testing? Static Analysis Static unit testing offers the minimum path coverage It helps in checking structural properties of code Here we check syntax, unreachable codes, undeclared/uninitialized variable, parameter type mismatch... Dynamic Analysis Dynamic unit testing detects control and data flow problems It helps in checking the behavior of unit source code on test data Here we will check classes, methods, boundary condition, logic errors...
  • 6. U n i t T e s t i n g 1. If a unit is not working, it affects performance of complete system For example Consider any system say bike, car, mobile, human being anything. Suppose mobile screen (lcd/led) is not working then you will not be able to see menu over screen. Hence you can not work/operate. Why to do Unit Testing?
  • 7. U n i t T e s t i n g 2.There is strength in unity but not in unit. It means with unit testing we can detect defect and fix bugs easily. For example Police interrogate suspects individually to get some more clues. If different component/unit of a system works fine, then there are More possibility that system (combining all component) will also work fine Why to do Unit Testing?
  • 8. U n i t T e s t i n g Why to do Unit Testing? Easy to detect defect and debug
  • 9. U n i t T e s t i n g Benefits of Unit Testing? > It Saves time on debugging. Hence effort and money too. > It prepares the base for further testing like integration, functional Testing... > It enhances performance and provide obstacle free control. > It ensures repeatability of desired behavior > Unit Testing makes it easier to change and refactor code. > Unit Testing inspires confidence. > Unit Testing reduces the level of bugs in production code. > We will get instant feedback > It brings modularity in design
  • 10. U n i t T e s t i n g How to do Unit Testing We have to write tests to check that the functionality of a unit, often a class(or function/methods in the class) meet its requirement. Tests are written as the unit is developed. So this testing is done by software engineer while development The idea is that every time you make a change to the unit you rerun the tests and see if your change has broken anything. There are many tools and framework that will help us in unit testing.
  • 11. U n i t T e s t i n g For different technology we can use different unit testing framework. For example Java: JUnit, TestNG... C: Ctest, Cunit, Opmock ... Python: Unittest, Py.test, Nose... Visual Basic(VB6.0): vbunit, vbaunit, TinyUnit... PHP: PHPunit, SnapTest ... We can see complete list here http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks Tools & Framework
  • 12. Introduction to Junit JUnit: It is a framework for performing unit testing of Java programs. It is used to write repeatable tests. For this we need Eclipse IDE. . Eclipse contains a base workspace and an extensible plug-in system for customizing the environment. It is used to develop application in various languages... JUnit is linked as a JAR at compile-time. The framework resides under package junit.framework for JUnit 3.8 and earlier, and under package org.junit for JUnit 4 and later.
  • 13. History of Junit 1994 Sunit came into existence 1997 Initiation of Junit 2000 Junit Officially Released 2002 Eclipse came into existence
  • 15. @Test public void method() The @Test annotation identifies a method as a test method. @Test (expected = Exception.class) Fails, if the method does not throw the named exception. @Test (timeout=time in millisec) Fails, if the method/test timed out after specified time @Ignore Ignores the test method. This is useful when the underlying code has been changed and the test case has not yet been adapted. Or if the execution time of this test is too long to be included. Annotations in Junit
  • 16. @Before public void method() This method is executed before each test. It is used to prepare the test environment (e.g. read input data, initialize the class). @After public void method() This method is executed after each test. It is used to cleanup the test environment (e.g. delete temporary data, restore defaults). It can also save memory by cleaning up expensive memory structures. Annotations in Junit
  • 17. Annotations in Junit @BeforeClass public static void method() This method is executed once, before the start of all tests. It is used to perform time intensive activities, for example to connect to a database. Methods annotated with this annotation need to be defined as static to work with JUnit. @AfterClass public static void method() This method is executed once, after all tests have been finished. It is used to perform clean-up activities, for example to disconnect from a database. Methods annotated with this annotation need to be defined as static to work with JUnit.
  • 18. . There are several Juint Assertion methods . These are static methods defined in org.junit.Assert class An assertion is a statement of expected outcome. In other words, it is the expected result of your test. Assert Method
  • 19. Assert Method Most common of them are > Fail() - Fails test > assertNotNull(0) /assertNull(0) - assert if object is (not) null > assertFalse(b) /assertTrue(b) - asserts if b is false /true > assertEquals(a, b) - Compares two objects using equals() method > assertArrayEquals(a, b) - compares two arrays
  • 20. Lets do some basic testing in Eclipse IDE
  • 21. Best Practices In Unit Testing Isolate test from other environment, classes, tests. Self Descriptive > Name of variable, classes, methods...... Avoid conditional logics(if else...), nested loops Use various assertion method provided by framework. > Assertion message should tell the problem. One can include business logics, Separate the test based on type and business module