What is JUnit ?
JUnit is a unit testing framework for Java programming language. It plays a crucial
role test-driven development, and is a family of unit testing frameworks
collectively known as xUnit.
JUnit promotes the idea of "first testing then coding", which emphasizes on
setting up the test data for a piece of code that can be tested first and then
implemented. This approach is like "test a little, code a little, test a little, code a
little." It increases the productivity of the programmer and the stability of
program code, which in turn reduces the stress on the programmer and the time
spent on debugging.
Features of JUnit
JUnit is an open source framework, which is used for writing and running
tests.
Provides annotations to identify test methods.
Provides assertions for testing expected results.
Provides test runners for running test
JUnit tests allow you to write codes faster, which increases quality.
JUnit is elegantly simple. It is less complex and takes less time.
JUnit tests can be run automatically and they check their own results and
provide immediate feedback. There's no need to manually comb through a
report of test results.
JUnit tests can be organized into test suites containing test cases and even
other test suites.
JUnit shows test progress in a bar that is green if the test is running
smoothly, and it turns red when a test fails.
Annotations for Junit testing
@Test : This annotation is a replacement of org.junit.TestCase which
indicates that public void method to which it is attached can be executed as
a test Case.
@Before : This annotation is used if you want to execute some statement
such as preconditions before each test case.
@BeforeClass : This annotation is used if you want to execute some
statements before all the test cases for e.g. test connection must be
executed before all the test cases.
@After : This annotation can be used if you want to execute some
statements after each Test Case for e.g resetting variables, deleting
temporary files ,variables, etc.
@AfterClass : This annotation can be used if you want to execute some
statements after all test cases for e.g. Releasing resources after executing
all test cases.
@Ignores: This annotation can be used if you want to ignore some
statements during test execution for e.g. disabling some test cases during
test execution.
@Test(timeout=500): This annotation can be used if you want to set some
timeout during test execution for e.g. if you are working under some SLA
(Service level agreement), and tests need to be completed within some
specified time.
Download and Installation of JUnit
Eclipse has JUnit already included , therefore if you intend to use JUnit
within Eclipse you don’t have to download anything
Downloading :
You can download JUnit 5 from http://www.junit.org/index.htm in the
zipped format
Unzip the JUnit 5 zip file
Add junit-5.jar to the CLASSPATH
Test1.java
package test1;
/**
* @author LENOVO
*/
public class Test1 {
public int addNumber(int a , int b){
return a+b;
public String addString(String c, String e){
return c + e;
Test1IT.java
package test1;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* @author LENOVO
*/
public class Test1IT {
public Test1IT() {
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
@Before
public void setUp() {
@After
public void tearDown() {
/**
* Test of addNumber method, of class Test1.
*/
@Test
public void testAddNumber() {
int a = 2;
int b = 2;
Test1 test = new Test1();
int result = test.addNumber(a, b);
assertEquals(4,result);
/**
* Test of addString method, of class Test1.
*/
@Test
public void testAddString() {
Test1 test1 = new Test1();
String result = test1.addString("rohit", "rk");
assertEquals("rohitrk",result);