Home RoadMap Blog Contact us Learn

11.Core Java Concepts: Assertions

Core Java Concepts: Assertions

Core Java Concepts for Selenium Automation: Assertions

1. Using the assert Keyword

The assert keyword in Java is used for debugging purposes to test assumptions in code. It is generally used during development to check for conditions that should always be true. If the condition is false, the program will throw an AssertionError.

Assertions can be enabled or disabled during runtime. By default, they are disabled and need to be explicitly enabled with the -ea (enable assertions) option when running the program.

Example of assert:

public class AssertionExample {
    public static void main(String[] args) {
        int x = 5;
        assert x > 10 : "X should be greater than 10";
        System.out.println("This line will not execute if assertion fails.");
    }
}

To run this program with assertions enabled:

java -ea AssertionExample

2. Assertion Libraries: JUnit and TestNG

In unit testing frameworks such as JUnit and TestNG, assertions play a crucial role in validating the expected behavior of the code. These frameworks provide various assert methods that compare actual results with expected results.

Commonly Used Assertion Methods in JUnit:

  • assertEquals(expected, actual) - Checks if two values are equal.
  • assertTrue(condition) - Checks if a condition is true.
  • assertFalse(condition) - Checks if a condition is false.
  • assertNull(object) - Checks if an object is null.
  • assertNotNull(object) - Checks if an object is not null.

Example: JUnit Assertions

import org.junit.Assert;
import org.junit.Test;

public class TestExample {
    @Test
    public void testAddition() {
        int sum = 2 + 3;
        Assert.assertEquals(5, sum);
    }
    
    @Test
    public void testCondition() {
        boolean condition = (2 < 5);
        Assert.assertTrue(condition);
    }
}

Commonly Used Assertion Methods in TestNG:

  • assertEquals(actual, expected) - Validates that two values are equal.
  • assertTrue(condition) - Validates that a condition is true.
  • assertFalse(condition) - Validates that a condition is false.
  • assertNull(object) - Validates that an object is null.
  • assertNotNull(object) - Validates that an object is not null.

Example: TestNG Assertions

import org.testng.Assert;
import org.testng.annotations.Test;

public class TestExample {
    @Test
    public void testString() {
        String actual = "Hello";
        String expected = "Hello";
        Assert.assertEquals(actual, expected, "Strings should match");
    }

    @Test
    public void testNull() {
        String str = null;
        Assert.assertNull(str, "Object should be null");
    }
}
Quick Exercise: Write a JUnit test that checks if a list contains a specific element using assertTrue.

Conclusion

Assertions are critical for validating test conditions in Java, especially in Selenium automation, where assertions can verify that expected results match actual outcomes after performing actions on web elements.

Assertions in Java

Recent Posts