www.SunilOS.com 1
www.sunilos.com
www.raystec.com
JUnit 4
03/17/16
03/17/16 www.SunilOS.com 2
Introduction
JUnit is an open source Java testing framework
It is used to write and run repeatable tests by
Developers.
Testing done by JUnit is called Unit Testing.
JUnit was originally written by Erich Gamma and
Kent Beck.
Test A Class – AccountService
 Lets have a Class that
provides Account Services
 Here are methods to
perform account services.
o add
o delete
o update
o get
o list
o getBalance
03/17/16 www.SunilOS.com 3
AccountService
+add(dto)
+update(dto)
+delete(dto)
+get(id)
+list() : List
+getBalance() : double
AccountDTO
+accssor
+id
+number
+name
+type
+balance
Conventional Testing Approach
 Write test in the main method
 public class TestAccount {
o public static void main(String[] args) {
 AccountDTO dto = new AccountDTO();
 dto.setId(1);
 dto.setNumber("SB123456");
 dto.setName("sunRays Technologies");
 dto.setBalance(10000);
 AccountService.add(dto); //Test#1
 AccountService.update(dto); //Test#2
 AccountService.delete(dto); //Test#3
 dto = AccountService.get(1); //Test#4
o }
 }
03/17/16 www.SunilOS.com 4
Problem ?
 AccountService.add(dto); //Test#1
 AccountService.update(dto); //Test#2
 AccountService.delete(dto); //Test#3
 dto = AccountService.get(1); //Test#4
 Any exception on any Test will
terminate subsequent tests from the
execution.
 Say if Test#2 has exception then
Test#3 and Test#4 will not be
executed. If Test#1 has exception then
Test#2,#3, and #4 will not be executed.
03/17/16 www.SunilOS.com 5
Solution
JUnit Framework.
Prepare Test Programs.
Test program is called
Testcase.
One Model/Service class will
have one JUnit Testcase.
Testing is called Unit Testing.
03/17/16 www.SunilOS.com 6
What is Unit Testcase
A Unit Testcase is a test program to test a
Usecase functionality.
A Testcase tests the response of a single
method to a particular set of input.
Unit Testcases are written and run by
Developers.
03/17/16 www.SunilOS.com 7
What is JUnit
JUnit is an open source Java testing
framework
It is used to write and run repeatable tests
by Developers.
Testing done by JUnit is called Unit Testing.
JUnit was originally written by Erich Gamma
and Kent Beck.
03/17/16 www.SunilOS.com 8
Features of JUnit
Fail and Assertion methods to test expected
results.
Test suites to execute multiple test cases
together.
Graphical and textual test runners.
03/17/16 www.SunilOS.com 9
Version 4.0
Current version is 4.x.
Supports Annotations.
03/17/16 www.SunilOS.com 10
What is regression testing
When you test new functionality along with
old functionality, is called regression testing.
Suppose you recently have developed
fundTransfer() method, when you test
openAccount(), deposit() and
withdraw() methods along with newly
created fundTransfer() method then it is
called regression testing.
03/17/16 www.SunilOS.com 11
Add testcase
 public class AccountServiceTestcase {
o @Test
o public void testAdd() {
 AccountDTO dto = new AccountDTO();
 dto.setId(1);
 dto.setNumber("SB123456");
 AccountService.add(dto);
 dto = AccountService.get(1);
 // If DTO is null then record is not added and testcase is fail
 if(dto == null){
 fail("Account is NOT added");
 }
o }
 }
03/17/16 www.SunilOS.com 12
Add testcase assert
 public class AccountServiceTestcase {
o @Test
o public void testAdd() {
 AccountDTO dto = new AccountDTO();
 dto.setId(1);
 dto.setNumber("SB123456");
 AccountService.add(dto);
 dto = AccountService.get(1);
 // If DTO is null then record is not added and testcase is fail
 assertNull("Account is NOT deleted", dto);
o }
 }
03/17/16 www.SunilOS.com 13
Delete testcase assert
 public class AccountServiceTestcase {
o @Test
o public void testDelete() {
 AccountDTO dto = new AccountDTO();
 dto.setId(1);
 AccountService.delete(dto);
 dto = AccountService.get(1);
 // If DTO is NOT null then record is not deleted and testcase is fail
 assertNotNull("Account is NOT deleted", dto);
o }
 }
03/17/16 www.SunilOS.com 14
Assert Methods
assertEquals()
assertTrue()
assertFalse()
assertNotNull()
assertNull()
assertSame()
assertNotSame()
assertArrayEquals()
03/17/16 www.SunilOS.com 15
Annotations
 @Test: public void method, annotated by this
annotation is run as test case.
 @Ignore: It is used to temporary disable a test or group of
tests to be executed. If a class is annotated by @Ignore
then all test methods of this class will be ignored.
03/17/16 www.SunilOS.com 16
Lifecycle Methods
A JUnit class may contain life-cycle
methods.
These methods are defined by following
annotations.
o @BeforeClass
o @AfterClass
o @Before
o @After
03/17/16 www.SunilOS.com 17
JUnit LifeCycle
03/17/16 www.SunilOS.com 18
Life Cycle Annotation
 @BeforeClass: An annotated public static void no-
argument method will be executed before execution of any
test method in the class.
 @AfterClass: An annotated public static void no-
argument method will be executed after all test methods of
this class are executed.
 @Before: this method will be called before each test
method in the Class.
 @After: this method will be called after each test method
in the Class.
03/17/16 www.SunilOS.com 19
Test Suite
A test suite is a collection of test cases.
A test runner is software that runs tests and
reports results.
03/17/16 www.SunilOS.com 20
Test Suite
 public class AllTests {
 public static TestSuite getSuite() {
o TestSuite suite = new TestSuite(“Test All ");
o suite.addTestSuite(AccountServiceTestcase.class);
o suite.addTestSuite(UserServiceTestcase.class);
o return suite;
 }
03/17/16 www.SunilOS.com 21
Test Runner
public static void main(String args[]){
junit.textui.TestRunner.run(getSuite());
}
03/17/16 www.SunilOS.com 22
JUnit Dependency
Jar
o junit-4.12.jar
Maven Dependency
o <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.12</version>
o </dependency>
03/17/16 www.SunilOS.com 23
Disclaimer
This is an educational presentation to enhance the
skill of computer science students.
This presentation is available for free to computer
science students.
Some internet images from different URLs are
used in this presentation to simplify technical
examples and correlate examples with the real
world.
We are grateful to owners of these URLs and
pictures.
www.SunilOS.com 2403/17/16
Thank You!
www.SunilOS.com 25
www.SunilOS.com
03/17/16