It is an open-source testing framework for java programmers.
It is one of the unit testing frameworks. Current version is JUnit 5.
Process of checking the functionality of Application whether it is working as per requirements.
Testing of single entity (Class or Method) is called as unit testing.
To perform unit testing we need to create test cases.
The unit test case code ensures that the program logic works as expected.
Manual : Executing test case without any tool. Time consuming, less reliable.
Automated : Executing test case with tool such as Junit 5. It is fast and more reliable.
Consider the class JbkAdd having method add. Now we have to perform testing on add method, whether it is working correct or not.
package com.javabykiran.testing.manual;
public class JbkAdd {
int add(int a, int b) {
int c = a + b;
return c;
}
}
Now we will make separate Test class for JbkAdd as JbkManualTest and we will write test case for add method.
package com.javabykiran.testing.manual;
public class JbkManualTest {
static void addTest() {
JbkAdd a = new JbkAdd();
int result = a.add(12, 10);
// Expected 22
if (result == 22) {
System.out.println("Test case: Pass");
} else {
System.out.println("Test case: Fail");
}
}
public static void main(String[] args) {
JbkManualTest.addTest();
}
}
Output:
Test case: Pass
Requirements – Junit5.jar, Extend TestCase class
Follow Following Steps –
Add Junit jar into build path as –Right click on Project-> Properties-> Libraries-> Add Library-> JUnit-> Junit-5-> Ok.
Write Following code for JUnit Testing –
Consider the class JbkAdd having method add. Now we have to perform JUnit testing on add method, whether it is working correct or not.
package com.javabykiran.junit;
public class JbkAdd {
int add(int a, int b) {
int c = a + b;
return c;
}
}
Now we will make separate Test class for JbkAdd as JbkJunitTest and we will write test case for add method which would be positive.
package com.javabykiran.junit;
import junit.framework.TestCase;
public class JbkJunitTest extends TestCase {
JbkAdd a;
@Override
protected void setUp() throws Exception {
System.out.println("in a setUp...");
a = new JbkAdd();
}
public void testAdd() {
assertEquals(25, a.add(5, 20)); // positive
}
@Override
protected void tearDown() throws Exception {
System.out.println("in a tearDown...");
a = null;
}
}
Now we will make separate Test class for JbkAdd as JbkJunitTest and we will write test case for add method which would be Negative.
package com.javabykiran.junit;
import junit.framework.TestCase;
public class JbkJunitTest extends TestCase {
JbkAdd a;
@Override
protected void setUp() throws Exception {
System.out.println("in a setUp...");
a = new JbkAdd();
}
public void testAdd() {
assertEquals(25, a.add(3, 20)); // positive
}
@Override
protected void tearDown() throws Exception {
System.out.println("in a tearDown...");
a = null;
}
}
Now we will make separate Test class for JbkAdd as JbkJunitTest and we will write test case which consists of assertEquals as well as assertNotSame.
package com.javabykiran.junit;
import junit.framework.TestCase;
public class JbkJunitTest extends TestCase {
JbkAdd a;
@Override
protected void setUp() throws Exception {
System.out.println("in a setUp...");
a = new JbkAdd();
}
public void testAdd() {
assertEquals(25, a.add(5, 20)); // positive
assertNotSame(24, a.add(2, 20)); // negative
}
@Override
protected void tearDown() throws Exception {
System.out.println("in a tearDown...");
a = null;
}
}