How do you implement test cases using assertions in JUnit?

Table of Contents

Introduction

Assertions are an essential part of JUnit test cases, as they help verify that the actual output of your code matches the expected output. In JUnit, assertions are used to compare the expected results with the actual results returned by the code under test. This ensures that your application behaves as expected. JUnit provides a variety of assertion methods, including assertEquals, assertTrue, assertFalse, assertNotNull, and more, to check different conditions.

In this guide, we’ll cover the key assertion methods provided by JUnit and show you how to implement test cases using assertions to validate your code.

Key JUnit Assertion Methods

1. assertEquals()

The assertEquals() method is one of the most commonly used assertions in JUnit. It checks whether the expected value matches the actual value returned by the code under test. If the values are equal, the test passes; otherwise, it fails.

Example: Using assertEquals()

In this example:

  • The add() method of a calculator class is tested.
  • The test checks whether adding 2 and 3 results in 5.

2. assertTrue() and assertFalse()

The assertTrue() method checks if the condition provided evaluates to true. The test passes if the condition is true, and fails if it’s false. Similarly, assertFalse() checks if the condition is false.

Example: Using assertTrue() and assertFalse()

In these examples:

  • assertTrue() checks if the number is even.
  • assertFalse() checks if the number is not even.

3. assertNotNull() and assertNull()

The assertNotNull() assertion checks if an object is not null, while assertNull() checks if the object is null. These assertions are useful for verifying the existence or absence of objects.

Example: Using assertNotNull() and assertNull()

In these examples:

  • assertNotNull() ensures the person object is created successfully.
  • assertNull() ensures that a null object is handled properly.

4. assertSame() and assertNotSame()

The assertSame() assertion checks whether two references point to the exact same object in memory, while assertNotSame() ensures they do not.

Example: Using assertSame() and assertNotSame()

Here:

  • assertSame() confirms that both variables reference the same object.
  • assertNotSame() confirms that the two variables point to different objects.

5. assertArrayEquals()

The assertArrayEquals() method is used to compare arrays in JUnit tests. It checks whether two arrays are equal in size and content.

Example: Using assertArrayEquals()

In this example:

  • assertArrayEquals() verifies that the two arrays expected and actual are identical.

6. assertThrows()

The assertThrows() method is used to test whether a specific exception is thrown during the execution of a piece of code. This is particularly useful when you want to validate that error handling is working correctly.

Example: Using assertThrows()

In this example:

  • The test ensures that dividing by zero throws an ArithmeticException.
  • The assertThrows() method checks that the specified exception is thrown during the execution of the lambda expression.

7. assertThat() (with AssertJ)

JUnit also supports more expressive assertions using the AssertJ library, which can be used with assertThat() to perform fluent-style assertions on various types of objects. It provides better readability and more powerful features compared to standard JUnit assertions.

Example: Using assertThat() with AssertJ

In this example:

  • The assertThat() assertion checks that the string result contains the word "awesome".

Conclusion

Assertions are the backbone of JUnit test cases, allowing you to validate expected outcomes in your unit tests. Whether you’re comparing values with assertEquals(), testing boolean conditions with assertTrue() and assertFalse(), or checking for null values with assertNotNull() and assertNull(), JUnit offers a wide range of assertion methods to suit different testing needs. By using these assertions effectively, you can ensure your code works correctly, prevent regression issues, and improve the overall reliability of your application.

Similar Questions