Open In App

JUnit 5 – Assertions

JUnit is a robust and widely used testing framework for Java. This plays an important role in making sure the software is reliable. It provides developers with a great structure for creating and executing test cases. Junit features help to embrace a test-driven development approach, that ensures confidence in the application. One of the main things in Junit is assertions that help developers validate the output with the desired result. In this article, we will go through JUnit 5 Assertions in detail and explore their work.

Prerequisites

To work with JUnit 5 assertions, make sure you have the following:



JUnit 5 Assertions

The Assertions class in JUnit 5 has a set of methods that helps in making assertions in your tests. These help in comparing the code output with your desired result. These methods cover a wide range of conditions. These include equality, nullity, truthiness, and many more.

Common Assertions in JUnit 5

Below are common assertions in Junit.



Example of JUnit 5 Assertions

Below is the Junit code along with its respective Java code which demonstrates the working of all assertions stated above.




//Java file
public class MathOperations {
  
    public int add(int a, int b) {
        return a + b;
    }
  
    public int subtract(int a, int b) {
        return a - b;
    }
  
    public boolean isPositive(int number) {
        return number > 0;
    }
  
    public boolean isNegative(int number) {
        return number < 0;
    }
  
    public String getHello() {
        return "Hello";
    }
  
    public int[] getArray() {
        return new int[]{1, 2, 3};
    }
  
    public String getHelloString() {
        return new String("Hello");
    }
  
    public void throwArithmeticException() {
        int result = 1 / 0;
    }
}




//Junit file
import static org.junit.jupiter.api.Assertions.*;
  
import org.junit.jupiter.api.Test;
  
class AssertionExamplesTest {
  
    private MathOperations mathOperations = new MathOperations();
  
    // assertEquals
    @Test
    void testAssertEquals() {
        int expected = 5;
        int actual = mathOperations.add(2, 3);
        assertEquals(expected, actual);
    }
  
    // assertNotEquals
    @Test
    void testAssertNotEquals() {
        int expected = 5;
        int actual = mathOperations.add(2, 2);
        assertNotEquals(expected, actual);
    }
  
    // assertTrue
    @Test
    void testAssertTrue() {
        int number = 10;
        assertTrue(mathOperations.isPositive(number));
    }
  
    // assertFalse
    @Test
    void testAssertFalse() {
        int number = -5;
        assertFalse(mathOperations.isPositive(number));
    }
  
    // assertNull
    @Test
    void testAssertNull() {
        String str = null;
        assertNull(str);
    }
  
    // assertNotNull
    @Test
    void testAssertNotNull() {
        String str = mathOperations.getHello();
        assertNotNull(str);
    }
  
    // assertArrayEquals
    @Test
    void testAssertArrayEquals() {
        int[] expectedArray = {1, 2, 3};
        int[] actualArray = mathOperations.getArray();
        assertArrayEquals(expectedArray, actualArray);
    }
  
    // assertSame
    @Test
    void testAssertSame() {
        String text = mathOperations.getHello();
        String reference = text;
        assertSame(text, reference);
    }
  
    // assertNotSame
    @Test
    void testAssertNotSame() {
        String text1 = mathOperations.getHello();
        String text2 = mathOperations.getHelloString();
        assertNotSame(text1, text2);
    }
  
    // assertThrows
    @Test
    void testAssertThrows() {
        assertThrows(ArithmeticException.class, mathOperations::throwArithmeticException);
    }
  
}

Output:

Example of JUnit 5 Assertions.


Article Tags :