SlideShare a Scribd company logo
Ravi Kant Soni
Senior Software Engineer
i-Admin , Bangalore
i-Admin,Bangalore
1
WHAT IS TESTING
Testing is the process of checking the
functionality of the application whether it
is working as per requirements
2
i-Admin,Bangalore
WHAT IS JUNIT
DeFacto framework for developing unit
test in java
 Currently version 4.x
Developed by Erich Gamma & Kent Beck
Released under IBM, and hosted on
sourcefog
Promotes the idea of "first testing then
coding“ 3
i-Admin,Bangalore
FEATURES OF JUNIT
An open source framework which is used
for writing & running tests
Shows test progress in a bar that is green
if test is going fine and it turns red when a
test fails
Provides Annotation & Assertions
4
i-Admin,Bangalore
WHAT IS UNIT TEST CASES?
Unit testing is the testing of single entity
(class or method)
A Unit Test Case is a part of code which
ensures that the another part of code
(method) works as expected
There must be at least two test cases for
each requirement: one positive test and
one negative test 5
i-Admin,Bangalore
JUNIT ENVIRONMENT SETUP
JUnit is a framework for Java, so the very
first requirement is to have JDK installed
in your machine.
Download latest version of JUnit jar file
from http://www.junit.org.
6
i-Admin,Bangalore
SET ECLIPSE ENVIRONMENT
7
i-Admin,Bangalore
JUNIT 4
8
i-Admin,Bangalore
Automatically recognizes test methods
preceded by @Test annotation
No need for main() to run the tests
Test methods must be public, void, with
no parameters
EXAMPLE
import org.junit.Test; // for @Test
import static org.junit.Assert.assertEquals; // assertEquals()
public class TestJunit {
@Test
public void testAdd() {
assertEquals(2, 2 + 1);
}
}
9
i-Admin,Bangalore
JUNIT TEST OUTPUT
10
i-Admin,Bangalore
JUNIT TEST OUTPUT
11
i-Admin,Bangalore
JUNIT TEST OUTPUT
12
i-Admin,Bangalore
Runs
Total number of tests run
Failures
Tests that failed. For example assertions that
failed.
Errors
Tests that generated unhandled (unexpected)
exceptions.
Time elapsed (ms)
JUNIT CLASSES
JUnit classes are important classes which is
used in writing and testing Junits
Assert which contain a set of assert
methods
TestCase which contain a test case defines
the fixture to run multiple tests
13
i-Admin,Bangalore
ASSERT
org.junit.Assert.* static methods:
 void assertEquals(expected, actual)
 Works with object, int, long, byte, string, etc
 Object: it invokes object.equals(object) for equality
 assertEquals (expected, actual, εpsilon)
 float and double
 void assertFalse/ True(boolean condition)
 void assertNull/ NotNull(Object object)
 void fail() 14
i-Admin,Bangalore
ASSERT EXAMPLE
i-Admin,Bangalore
15
ANNOTATION
Annotations are like meta-tags
 @Test
 @Before
 @After
 @BeforeClass
 @AfterClass
 @Ignore
16
i-Admin,Bangalore
ANNOTATION EXAMPLE
i-Admin,Bangalore
17
PARAMETERIZED TEST
 Allows you to run the same test with different
data
 @RunWith(Parameterized.class)
 public static method that returns a Collection
of data
 Collection must be an Array of the various
parameters used for the test
 public constructor that uses the parameters 18
i-Admin,Bangalore
PARAMETERIZED EXAMPLE
i-Admin,Bangalore
19
SUITES
 Specify an execution order
 Add @Suite to an empty class
@RunWith(Suite.class)
@Suite.SuiteClasses({SomeTest.class})
public class AllTests { }
20
i-Admin,Bangalore
SUITES EXAMPLE
i-Admin,Bangalore
21
TESTING EXCEPTIONS
i-Admin,Bangalore
22
GOOD UNIT TEST
 Any static utility method must have test
 Make exception tests
 Business models with equals and hashcode
 Test that “something is true” but also that
“not-something is false”
 Give your tests meaningful names
23
i-Admin,Bangalore
MORE RESOURCE
 Official site: www.junit.org
 JUnit cook book
http://junit.sourceforge.net/doc/cookbook
/cookbook.htm (one recipe long!)
 JUnit Javadoc:
http://junit.org/junit/javadoc/4.5/
24
i-Admin,Bangalore
i-Admin,Bangalore
25

More Related Content

PPTX
Unit testing
PDF
Unit Testing Guidelines
PPTX
Unit Tests And Automated Testing
PPTX
Unit tests & TDD
PPS
Unit Testing
PPTX
Test driven development in .Net - 2010 + Eclipse
PPTX
Unit test
PPTX
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Unit testing
Unit Testing Guidelines
Unit Tests And Automated Testing
Unit tests & TDD
Unit Testing
Test driven development in .Net - 2010 + Eclipse
Unit test
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

What's hot (18)

PPTX
An Introduction to Unit Testing
PPSX
Unit Test Presentation
PPTX
Unit Testing And Mocking
PPTX
Unit Testing (C#)
PPTX
Software testing ... who’s responsible is it?
PDF
Unit and integration Testing
PPTX
Unit Testing Concepts and Best Practices
PPTX
Unit Testing with Python
PDF
iOS Test-Driven Development
PDF
Workshop unit test
PDF
Software Testing
PPTX
Benefit From Unit Testing In The Real World
PPS
Why Unit Testingl
PPTX
Unit Testing in Action - C#, NUnit, and Moq
PPTX
White box testing
PPTX
Understanding Unit Testing
PPTX
Test-Driven Development
PPTX
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
An Introduction to Unit Testing
Unit Test Presentation
Unit Testing And Mocking
Unit Testing (C#)
Software testing ... who’s responsible is it?
Unit and integration Testing
Unit Testing Concepts and Best Practices
Unit Testing with Python
iOS Test-Driven Development
Workshop unit test
Software Testing
Benefit From Unit Testing In The Real World
Why Unit Testingl
Unit Testing in Action - C#, NUnit, and Moq
White box testing
Understanding Unit Testing
Test-Driven Development
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Ad

Viewers also liked (17)

DOC
Диплом Никифорова А.
DOCX
Religion
PPTX
Oktaviani sari
PPTX
Zed ria presentation
PPTX
PDF
Matilla Portfolio
DOCX
Курсовая Сланова Н.
PPTX
Data driven decision-making - Students' Unions 2013
PDF
Padur flower presentation sujitha
PPTX
портфоліо на мк 2013 [автосохраненный] готовий
DOC
Курсовая Хананушан Н.
DOCX
Совершенствование методов фестивальной оценки рекламной деятельности (на при...
PDF
Pragati a4 brouchre - wide
PPTX
Pp pidato
PPTX
Pp pidato
PDF
Spring MVC 3.0 Framework (sesson_2)
DOCX
Диплом Пакалина Ю.
Диплом Никифорова А.
Religion
Oktaviani sari
Zed ria presentation
Matilla Portfolio
Курсовая Сланова Н.
Data driven decision-making - Students' Unions 2013
Padur flower presentation sujitha
портфоліо на мк 2013 [автосохраненный] готовий
Курсовая Хананушан Н.
Совершенствование методов фестивальной оценки рекламной деятельности (на при...
Pragati a4 brouchre - wide
Pp pidato
Pp pidato
Spring MVC 3.0 Framework (sesson_2)
Диплом Пакалина Ю.
Ad

Similar to Junit (20)

PDF
SE2_Lec 21_ TDD and Junit
PPS
Why Unit Testingl
PPS
Why unit testingl
PDF
Unit testing, principles
PPTX
Upstate CSCI 540 Unit testing
PPT
Junit Interview Questions-ppt
PPTX
Unit Testing in .NET Core 7.0 with XUnit.pptx
PDF
Testing on frontend
PDF
Unit Testing in Software Development: Why It Matters and How to Do It Right
PDF
Introduction To UnitTesting & JUnit
PPTX
8-testing.pptx
PPTX
Junit and cactus
PPT
Google test training
DOCX
JUnit_Guide_Expanded_Presentation[1].docx
DOCX
JUnit_Guide_Expanded_Presentation[1].docx............................
DOCX
JUnit_Guide_Expanded_Presentation[1].docx
PPT
Unit testing php-unit - phing - selenium_v2
PPT
Testing Options in Java
PPTX
Unit Testing in Java
PDF
TDD Workshop UTN 2012
SE2_Lec 21_ TDD and Junit
Why Unit Testingl
Why unit testingl
Unit testing, principles
Upstate CSCI 540 Unit testing
Junit Interview Questions-ppt
Unit Testing in .NET Core 7.0 with XUnit.pptx
Testing on frontend
Unit Testing in Software Development: Why It Matters and How to Do It Right
Introduction To UnitTesting & JUnit
8-testing.pptx
Junit and cactus
Google test training
JUnit_Guide_Expanded_Presentation[1].docx
JUnit_Guide_Expanded_Presentation[1].docx............................
JUnit_Guide_Expanded_Presentation[1].docx
Unit testing php-unit - phing - selenium_v2
Testing Options in Java
Unit Testing in Java
TDD Workshop UTN 2012

Recently uploaded (20)

PPTX
sap open course for s4hana steps from ECC to s4
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Empathic Computing: Creating Shared Understanding
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Encapsulation theory and applications.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Big Data Technologies - Introduction.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Cloud computing and distributed systems.
sap open course for s4hana steps from ECC to s4
Encapsulation_ Review paper, used for researhc scholars
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Empathic Computing: Creating Shared Understanding
Network Security Unit 5.pdf for BCA BBA.
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Spectral efficient network and resource selection model in 5G networks
Mobile App Security Testing_ A Comprehensive Guide.pdf
cuic standard and advanced reporting.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Encapsulation theory and applications.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
MYSQL Presentation for SQL database connectivity
Big Data Technologies - Introduction.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Cloud computing and distributed systems.

Junit