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

<table data-full-width="true"><thead><tr><th>Manual Testing</th><th>Automated Testing</th></tr></thead><tbody><tr><td>Executing a test cases manually without any tool support is known as manual testing.</td><td>Taking tool support and executing the test cases by using an automation tool is known as automation testing.</td></tr><tr><td>Time-consuming and tedious − Since test cases are executed by human resources, it is very slow and tedious.</td><td>Fast − Automation runs test cases significantly faster than human resources.</td></tr><tr><td>Huge investment in human resources − As test cases need to be executed manually, more testers are required in manual testing.</td><td>Less investment in human resources − Test cases are executed using automation tools, so less number of testers are required in automation testing.</td></tr><tr><td>Less reliable − Manual testing is less reliable, as it has to account for human errors.</td><td>More reliable − Automation tests are precise and reliable.</td></tr><tr><td>Non-programmable − No programming can be done to write sophisticated tests to fetch hidden information.</td><td>Programmable − Testers can program sophisticated tests to bring out hidden information.</td></tr></tbody></table>

### What is JUnit ?

JUnit is a <mark style="background-color:orange;">**unit testing framework**</mark> 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

<table data-full-width="true"><thead><tr><th>Assertion</th><th>Explanation</th><th>Rules</th><th>Hard/Soft</th></tr></thead><tbody><tr><td><code>assertEquals()</code></td><td>Checks if two values are equal.</td><td>The actual and expected values must be of the same data type.</td><td>Hard</td></tr><tr><td><code>assertTrue()</code></td><td>Checks if a boolean value is true.</td><td>The actual value must be a boolean.</td><td>Hard</td></tr><tr><td><code>assertFalse()</code></td><td>Checks if a boolean value is false.</td><td>The actual value must be a boolean.</td><td>Hard</td></tr><tr><td><code>assertNull()</code></td><td>Checks if a value is null.</td><td>The actual value must be null.</td><td>Hard</td></tr><tr><td><code>assertNotNull()</code></td><td>Checks if a value is not null.</td><td>The actual value must not be null.</td><td>Hard</td></tr><tr><td><code>assertSame()</code></td><td>Checks if two objects refer to the same object.</td><td>The actual and expected values must be the same object.</td><td>Hard</td></tr><tr><td><code>assertNotSame()</code></td><td>Checks if two objects do not refer to the same object.</td><td>The actual and expected values must not be the same object.</td><td>Hard</td></tr><tr><td><code>assertArrayEquals()</code></td><td>Checks if two arrays are equal.</td><td>The actual and expected arrays must have the same length and contain the same elements in the same order.</td><td>Hard</td></tr><tr><td><code>assertThat()</code></td><td>Checks if a value meets a specified condition.</td><td>The actual value must meet the specified condition.</td><td>Soft</td></tr></tbody></table>

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:

```java
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

<table data-full-width="true"><thead><tr><th>Assertion</th><th>Framework</th><th>Explanation</th></tr></thead><tbody><tr><td><code>assertArrayEquals()</code></td><td>JUnit</td><td>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.</td></tr><tr><td><code>assertSame()</code></td><td>JUnit</td><td>Checks if two objects refer to the same object. The actual and expected values must be the same object.</td></tr><tr><td><code>assertNotSame()</code></td><td>JUnit</td><td>Checks if two objects do not refer to the same object. The actual and expected values must not be the same object.</td></tr><tr><td><code>assertThat()</code></td><td>JUnit</td><td>Checks if a value meets a specified condition. The actual value must meet the specified condition.</td></tr><tr><td><code>assertThrows()</code></td><td>JUnit</td><td>Checks if a specific exception is thrown by the code being tested.</td></tr><tr><td><code>assertAll()</code></td><td>JUnit</td><td>Groups multiple assertions together and reports all failures at once.</td></tr><tr><td><code>assertTimeout()</code></td><td>JUnit</td><td>Checks if a piece of code executes within a specified time limit.</td></tr><tr><td><code>assertTimeoutPreemptively()</code></td><td>JUnit</td><td>Checks if a piece of code executes within a specified time limit, and aborts the code if it takes too long.</td></tr><tr><td><code>SoftAssert</code></td><td>TestNG</td><td>Allows for soft assertions, which do not throw an exception when they fail. Instead, they log the failure and continue with the test.</td></tr><tr><td><code>assertNotEquals()</code></td><td>TestNG</td><td>Checks if two values are not equal.</td></tr><tr><td><code>assertNotSame()</code></td><td>TestNG</td><td>Checks if two objects do not refer to the same object. The actual and expected values must not be the same object.</td></tr><tr><td><code>assertContains()</code></td><td>TestNG</td><td>Checks if a string contains a specified substring.</td></tr><tr><td><code>assertNotContains()</code></td><td>TestNG</td><td>Checks if a string does not contain a specified substring.</td></tr></tbody></table>

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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://qatesting.gitbook.io/qa/automation-testing/flow/selenium/testing-frameworks/junit.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
