🉑JUnit

JUnit - Overview

Testing is the process of checking the functionality of an application to ensure it runs as per requirements. Unit testing comes into picture at the developers’ level; it is the testing of single entity (class or method). Unit testing plays a critical role in helping a software company deliver quality products to its customers.

Unit testing can be done in two ways − manual testing and automated testing.

Manual TestingAutomated Testing

Executing a test cases manually without any tool support is known as manual testing.

Taking tool support and executing the test cases by using an automation tool is known as automation testing.

Time-consuming and tedious − Since test cases are executed by human resources, it is very slow and tedious.

Fast − Automation runs test cases significantly faster than human resources.

Huge investment in human resources − As test cases need to be executed manually, more testers are required in manual testing.

Less investment in human resources − Test cases are executed using automation tools, so less number of testers are required in automation testing.

Less reliable − Manual testing is less reliable, as it has to account for human errors.

More reliable − Automation tests are precise and reliable.

Non-programmable − No programming can be done to write sophisticated tests to fetch hidden information.

Programmable − Testers can program sophisticated tests to bring out hidden information.

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

  • JUnit shows test progress in a bar that is green if the test is running smoothly, and it turns red when a test fails.

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 Java programming language.

A formal written unit test case is characterized by a known input and an expected output, which is worked out before the test is executed. The known input should test a precondition and the expected output should test a post-condition.

There must be at least two unit test cases for each requirement − one positive test and one negative test. If a requirement has sub-requirements, each sub-requirement must have at least two test cases as positive and negative.

JUnit Assertions

AssertionExplanationRulesHard/Soft

assertEquals()

Checks if two values are equal.

The actual and expected values must be of the same data type.

Hard

assertTrue()

Checks if a boolean value is true.

The actual value must be a boolean.

Hard

assertFalse()

Checks if a boolean value is false.

The actual value must be a boolean.

Hard

assertNull()

Checks if a value is null.

The actual value must be null.

Hard

assertNotNull()

Checks if a value is not null.

The actual value must not be null.

Hard

assertSame()

Checks if two objects refer to the same object.

The actual and expected values must be the same object.

Hard

assertNotSame()

Checks if two objects do not refer to the same object.

The actual and expected values must not be the same object.

Hard

assertArrayEquals()

Checks if two arrays are equal.

The actual and expected arrays must have the same length and contain the same elements in the same order.

Hard

assertThat()

Checks if a value meets a specified condition.

The actual value must meet the specified condition.

Soft

Note: assertThat() is a soft assertion, meaning that it does not throw an exception when it fails. Instead, it logs the failure and continues with the test.

Here's a Java code block showing examples of each assertion:

import org.junit.Test;
import static org.junit.Assert.*;

public class JUnitAssertionsTest {

    @Test
    public void testAssertEquals() {
        assertEquals(2, 1 + 1);
    }

    @Test
    public void testAssertTrue() {
        assertTrue(1 + 1 == 2);
    }

    @Test
    public void testAssertFalse() {
        assertFalse(1 + 1 == 3);
    }

    @Test
    public void testAssertNull() {
        Object obj = null;
        assertNull(obj);
    }

    @Test
    public void testAssertNotNull() {
        Object obj = new Object();
        assertNotNull(obj);
    }

    @Test
    public void testAssertSame() {
        Object obj1 = new Object();
        Object obj2 = obj1;
        assertSame(obj1, obj2);
    }

    @Test
    public void testAssertNotSame() {
        Object obj1 = new Object();
        Object obj2 = new Object();
        assertNotSame(obj1, obj2);
    }

    @Test
    public void testAssertArrayEquals() {
        int[] expected = {1, 2, 3};
        int[] actual = {1, 2, 3};
        assertArrayEquals(expected, actual);
    }

    @Test
    public void testAssertThat() {
        int num = 5;
        assertThat(num, greaterThan(4));
    }
}

Assertions that are only found in JUnit and TestNG

AssertionFrameworkExplanation

assertArrayEquals()

JUnit

Checks if two arrays are equal. The actual and expected arrays must have the same length and contain the same elements in the same order.

assertSame()

JUnit

Checks if two objects refer to the same object. The actual and expected values must be the same object.

assertNotSame()

JUnit

Checks if two objects do not refer to the same object. The actual and expected values must not be the same object.

assertThat()

JUnit

Checks if a value meets a specified condition. The actual value must meet the specified condition.

assertThrows()

JUnit

Checks if a specific exception is thrown by the code being tested.

assertAll()

JUnit

Groups multiple assertions together and reports all failures at once.

assertTimeout()

JUnit

Checks if a piece of code executes within a specified time limit.

assertTimeoutPreemptively()

JUnit

Checks if a piece of code executes within a specified time limit, and aborts the code if it takes too long.

SoftAssert

TestNG

Allows for soft assertions, which do not throw an exception when they fail. Instead, they log the failure and continue with the test.

assertNotEquals()

TestNG

Checks if two values are not equal.

assertNotSame()

TestNG

Checks if two objects do not refer to the same object. The actual and expected values must not be the same object.

assertContains()

TestNG

Checks if a string contains a specified substring.

assertNotContains()

TestNG

Checks if a string does not contain a specified substring.

Note: The assertNotSame() assertion is available in both JUnit and TestNG, but it has slightly different behavior in each framework. In JUnit, it checks if two objects do not refer to the same object, while in TestNG, it checks if two objects are not equal.

Last updated