Junit

Nmudaykumar
3 min readJan 16, 2022

--

JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development and is one of a family of unit testing frameworks collectively known as xUnit, that originated with JUnit.

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 tests.
  • 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.

What is a Unit Test Case?

A Unit Test Case is a part of code, which ensures that another part of code (method) works as expected. To achieve the desired results quickly, a test framework is required. JUnit is a perfect unit test framework for the Java programming language.

Types of unit testing

There are two ways to perform unit testing: 1) manual testing 2) automated testing.

1) Manual Testing

If you execute the test cases manually without any tool support, it is known as manual testing. It is time-consuming and less reliable.

2) Automated Testing

If you execute the test cases by tool support, it is known as automated testing. It is fast and more reliable.

Annotations for Junit testing

The Junit 4.x framework is annotation-based, so let’s see the annotations that can be used while writing the test cases.

@Test annotation specifies that method is the test method.

@Test(timeout=1000) annotation specifies that the method will be failed if it takes longer than 1000 milliseconds (1 second).

@BeforeClass annotation specifies that the method will be invoked only once, before starting all the tests.

@Before annotation specifies that method will be invoked before each test.

@After annotation specifies that method will be invoked after each test.

@AfterClass annotation specifies that the method will be invoked only once, after finishing all the tests.

Example:-

import static org.junit.Assert.assertEquals;

import org.junit.After;

import org.junit.AfterClass;

import org.junit.Before;

import org.junit.BeforeClass;

import org.junit.Test;

public class TestCase2 {

@BeforeClass

public static void setUpBeforeClass() throws Exception {

System.out.println(“before class”);

}

@Before

public void setUp() throws Exception {

System.out.println(“before”);

}

@Test

public void testFindMax(){

System.out.println(“test case find max”);

assertEquals(4,Calculation.findMax(new int[]{1,3,4,2}));

assertEquals(-2,Calculation.findMax(new int[]{-12,-3,-4,-2}));

}

@Test

public void testCube(){

System.out.println(“test case cube”);

assertEquals(27,Calculation.cube(3));

}

@Test

public void testReverseWord(){

System.out.println(“test case reverse word”);

assertEquals(“ym eman si nahk”,Calculation.reverseWord(“my name is khan”);

}

@After

public void tearDown() throws Exception {

System.out.println(“after”);

}

@AfterClass

public static void tearDownAfterClass() throws Exception {

System.out.println(“after class”);

}

}

Assert class

The org.JUnit.Assert class provides methods to assert the program logic.

Methods of Assert class

The common methods of the Assert class are as follows:

  1. void assertEquals(boolean expected, boolean actual): checks that two primitives/objects are equal. It is overloaded.
  2. void assertTrue(boolean condition): checks that a condition is true.
  3. void assertFalse(boolean condition): checks that a condition is false.
  4. void assertNull(Object obj): checks that the object is null.
  5. void assertNotNull(Object obj): checks that object is not null.

Example

import static org.junit.Assert.*;

import org.junit.Test;

public class TestLogic {

@Test

public void testFindMax(){

assertEquals(4,Calculation.findMax(new int[]{1,3,4,2}));

assertEquals(-1,Calculation.findMax(new int[]{-12,-1,-3,-4,-2}));

}

}

--

--