junit
DESCRIPTION
JUnit is a framework for Java, so the very first requirement is to have JDK installed in your machine. 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 failsTRANSCRIPT
![Page 1: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/1.jpg)
Ravi Kant Soni
Senior Software Engineer
i-Admin , Bangalore
i-Ad
min
, Ban
galore
1
![Page 2: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/2.jpg)
WHAT IS TESTING
Testing is the process of checking the functionality of the application whether it is working as per requirements
2
i-Ad
min
, Ban
galore
![Page 3: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/3.jpg)
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-Ad
min
, Ban
galore
![Page 4: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/4.jpg)
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-Ad
min
, Ban
galore
![Page 5: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/5.jpg)
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-Ad
min
, Ban
galore
![Page 6: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/6.jpg)
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-Ad
min
, Ban
galore
![Page 7: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/7.jpg)
SET ECLIPSE ENVIRONMENT
7
i-Ad
min
, Ban
galore
![Page 8: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/8.jpg)
JUNIT 4
8
i-Ad
min
, Ban
galore
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
![Page 9: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/9.jpg)
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-Ad
min
, Ban
galore
![Page 10: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/10.jpg)
JUNIT TEST OUTPUT
10
i-Ad
min
, Ban
galore
![Page 11: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/11.jpg)
JUNIT TEST OUTPUT
11
i-Ad
min
, Ban
galore
![Page 12: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/12.jpg)
JUNIT TEST OUTPUT
12
i-Ad
min
, Ban
galore
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)
![Page 13: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/13.jpg)
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-Ad
min
, Ban
galore
![Page 14: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/14.jpg)
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-Ad
min
, Ban
galore
![Page 15: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/15.jpg)
ASSERT EXAMPLE
i-Ad
min
, Ban
galore
15
![Page 16: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/16.jpg)
ANNOTATION
Annotations are like meta-tags
@Test
@Before
@After
@BeforeClass
@AfterClass
@Ignore 16
i-Ad
min
, Ban
galore
![Page 17: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/17.jpg)
ANNOTATION EXAMPLE
i-Ad
min
, Ban
galore
17
![Page 18: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/18.jpg)
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-Ad
min
, Ban
galore
![Page 19: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/19.jpg)
PARAMETERIZED EXAMPLE
i-Ad
min
, Ban
galore
19
![Page 20: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/20.jpg)
SUITES
Specify an execution order
Add @Suite to an empty class
@RunWith(Suite.class)
@Suite.SuiteClasses({SomeTest.class})
public class AllTests { }
20
i-Ad
min
, Ban
galore
![Page 21: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/21.jpg)
SUITES EXAMPLE
i-Ad
min
, Ban
galore
21
![Page 22: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/22.jpg)
TESTING EXCEPTIONS
i-Ad
min
, Ban
galore
22
![Page 23: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/23.jpg)
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-Ad
min
, Ban
galore
![Page 24: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/24.jpg)
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-Ad
min
, Ban
galore
![Page 25: Junit](https://reader033.vdocuments.us/reader033/viewer/2022060110/5562196cd8b42af2128b5587/html5/thumbnails/25.jpg)
i-Ad
min
, Ban
galore
25