In this section, we will explore the most commonly used assertions in JUnit. Assertions are essential for verifying that the code behaves as expected. They are the foundation of any unit test, allowing you to compare the actual output of your code against the expected output.

Key Concepts

  1. Assertions: Statements that check whether a condition is true. If the condition is false, the test fails.
  2. Common Assertions: Frequently used assertions provided by JUnit to validate different types of conditions.

Common Assertions in JUnit

JUnit provides a variety of assertions to help you validate your code. Here are some of the most commonly used ones:

  1. assertEquals

Checks if two values are equal.

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

public class ExampleTest {
    @Test
    public void testAddition() {
        int result = 2 + 3;
        assertEquals(5, result, "2 + 3 should equal 5");
    }
}

  1. assertNotEquals

Checks if two values are not equal.

import static org.junit.jupiter.api.Assertions.assertNotEquals;
import org.junit.jupiter.api.Test;

public class ExampleTest {
    @Test
    public void testSubtraction() {
        int result = 5 - 3;
        assertNotEquals(1, result, "5 - 3 should not equal 1");
    }
}

  1. assertTrue

Checks if a condition is true.

import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;

public class ExampleTest {
    @Test
    public void testIsPositive() {
        int number = 5;
        assertTrue(number > 0, "Number should be positive");
    }
}

  1. assertFalse

Checks if a condition is false.

import static org.junit.jupiter.api.Assertions.assertFalse;
import org.junit.jupiter.api.Test;

public class ExampleTest {
    @Test
    public void testIsNegative() {
        int number = -5;
        assertFalse(number > 0, "Number should not be positive");
    }
}

  1. assertNull

Checks if an object is null.

import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.jupiter.api.Test;

public class ExampleTest {
    @Test
    public void testObjectIsNull() {
        Object obj = null;
        assertNull(obj, "Object should be null");
    }
}

  1. assertNotNull

Checks if an object is not null.

import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Test;

public class ExampleTest {
    @Test
    public void testObjectIsNotNull() {
        Object obj = new Object();
        assertNotNull(obj, "Object should not be null");
    }
}

  1. assertArrayEquals

Checks if two arrays are equal.

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;

public class ExampleTest {
    @Test
    public void testArrayEquality() {
        int[] expected = {1, 2, 3};
        int[] actual = {1, 2, 3};
        assertArrayEquals(expected, actual, "Arrays should be equal");
    }
}

  1. assertSame

Checks if two references point to the same object.

import static org.junit.jupiter.api.Assertions.assertSame;
import org.junit.jupiter.api.Test;

public class ExampleTest {
    @Test
    public void testSameObject() {
        Object obj1 = new Object();
        Object obj2 = obj1;
        assertSame(obj1, obj2, "Both references should point to the same object");
    }
}

  1. assertNotSame

Checks if two references do not point to the same object.

import static org.junit.jupiter.api.Assertions.assertNotSame;
import org.junit.jupiter.api.Test;

public class ExampleTest {
    @Test
    public void testDifferentObjects() {
        Object obj1 = new Object();
        Object obj2 = new Object();
        assertNotSame(obj1, obj2, "References should point to different objects");
    }
}

Practical Exercise

Exercise 1: Basic Assertions

Write a JUnit test to verify the following conditions:

  1. The sum of 10 and 20 is 30.
  2. The difference between 20 and 10 is not 15.
  3. A string "JUnit" is not null.
  4. An array {1, 2, 3} is equal to another array {1, 2, 3}.

Solution

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class BasicAssertionsTest {
    @Test
    public void testBasicAssertions() {
        // 1. The sum of 10 and 20 is 30
        assertEquals(30, 10 + 20, "10 + 20 should equal 30");

        // 2. The difference between 20 and 10 is not 15
        assertNotEquals(15, 20 - 10, "20 - 10 should not equal 15");

        // 3. A string "JUnit" is not null
        String str = "JUnit";
        assertNotNull(str, "String should not be null");

        // 4. An array {1, 2, 3} is equal to another array {1, 2, 3}
        int[] expectedArray = {1, 2, 3};
        int[] actualArray = {1, 2, 3};
        assertArrayEquals(expectedArray, actualArray, "Arrays should be equal");
    }
}

Summary

In this section, we covered the most commonly used assertions in JUnit, including assertEquals, assertNotEquals, assertTrue, assertFalse, assertNull, assertNotNull, assertArrayEquals, assertSame, and assertNotSame. These assertions are fundamental for writing effective unit tests, allowing you to validate various conditions in your code. In the next section, we will delve into advanced assertions to further enhance your testing capabilities.

© Copyright 2024. All rights reserved