JUnit Testing Best Practices: Verifying No Exceptions in Your Java Code

Introduction

When writing tests in Java, it’s important to verify that a method or code block behaves as expected. One of the most common things to check is whether an exception is thrown during the execution of a test. However, sometimes we also want to verify that no exception is thrown. This can be particularly important in situations where an exception could cause a critical failure or an unexpected behavior. In this tutorial, we’ll go through how to assert that no exception has been thrown in a JUnit test using both JUnit 4 and JUnit 5.

Asserting in JUnit 4

In JUnit 4, we can use the @Test annotation to mark a method as a test. To assert that no exception is thrown, we can use the @Test annotation’s expected attribute. Here’s an example:

import org.junit.Test;

public class MyTest {
    
    @Test(expected = NoException.class)
    public void testMethod() {
        // Code that should not throw an exception goes here
    }
    
}

In this example, we’re using the @Test annotation to mark the testMethod as a test. We’re also using the expected attribute to specify that we expect no exception to be thrown during the execution of testMethod. If an exception is thrown during the execution of testMethod, JUnit will mark the test as failed.

Asserting in JUnit 5

In JUnit 5, we can use the @Test annotation in a similar way as JUnit 4. However, instead of using the expected attribute, we can use the assertDoesNotThrow method from the Assertions class. Here’s an example:

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

public class MyTest {
    
    @Test
    public void testMethod() {
        assertDoesNotThrow(() -> {
            // Code that should not throw an exception goes here
        });
    }
    
}

In this example, we’re using the @Test annotation to mark the testMethod as a test. We’re also using the assertDoesNotThrow method to specify that we expect no exception to be thrown during the execution of the code block passed as a lambda to the assertDoesNotThrow method. If an exception is thrown during the execution of the code block, JUnit will mark the test as failed.

Conclusion

Asserting that no exception is thrown during the execution of a test can be important for ensuring that a method or code block behaves as expected. In JUnit 4, we can use the @Test annotation’s expected attribute to specify that we expect no exception to be thrown. In JUnit 5, we can use the assertDoesNotThrow method from the Assertions class to achieve the same result. Remember to test your code thoroughly to ensure that it behaves as expected in all situations.

Found the article helpful? if so please follow us on Socials