JUnit Testing Exceptions in JUnit Estimated reading: 2 minutes 161 views Handling exceptions correctly is crucial in web application testing. JUnit allows us to verify whether methods throw expected exceptions, ensuring our application responds well to edge cases and incorrect inputs. In this guide, we will use a real web test case and modify it to trigger and validate exceptions in JUnit.Testing Exceptions with assertThrowsJUnit provides the assertThrows method to verify that a method throws a specific exception when executed. Let’s apply this to our Locked Screen Page test to check for scenarios where an element is not found.JUnit assertThrows Exception Testpackage com.mqa.junit.tests; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; class LockedScreenExceptionTest { private WebDriver driver; @BeforeEach void setup() { System.setProperty("webdriver.chrome.driver", "src/test/resources/driver/chromedriver"); driver = new ChromeDriver(); driver.get("https://jahmalrichard.github.io/mqa-demo-test-app/pages-lock-screen.html"); } @Test void testElementNotFoundException() { // Attempt to find a non-existent element and expect an exception assertThrows(NoSuchElementException.class, () -> { driver.findElement(By.id("username")); }, "Expected NoSuchElementException when element is not found"); } @Test void testIncorrectUrlAccess() { // Intentionally navigating to a wrong URL and checking an exception driver.get("https://jahmalrichard.github.io/mqa-demo-test-app/login.html"); assertThrows(NoSuchElementException.class, () -> { driver.findElement(By.className("user-name")); }, "Expected NoSuchElementException when trying to find an element on a non-existent page"); } @AfterEach void teardown() { driver.quit(); } }Explanation of the Exception Tests testElementNotFoundException() Tries to find an element with a non-existent ID.Uses assertThrows to check if NoSuchElementException is thrown.Ensures proper exception handling when an element is missing. testIncorrectUrlAccess() Navigates to an invalid webpage.Attempts to find an element, which should not exist.Uses assertThrows to verify that NoSuchElementException is thrown.Why Exception Testing Matters Ensures the application fails gracefully when unexpected situations occur. Helps prevent hard crashes by catching and handling errors properly. Strengthens test coverage by accounting for edge cases and failures.ConclusionTesting exceptions in JUnit ensures that your application correctly handles error scenarios. By using assertThrows, you can write clear and maintainable exception tests. In the next guide, we will explain Using JUnit Test Suites to Organize Tests to help manage multiple test cases efficiently.Tagged:JUnit JUnit - Previous Understanding JUnit Assertions and Annotations Next - JUnit Using JUnit Test Suites to Organize Tests