JUnit 5 Expected Exception

In JUnit 5, we can use assertThrows to assert an exception is thrown. P.S Tested with JUnit 5.5.2 1. Unchecked Exception 1.1 JUnit example of catching a runtime exception. ExceptionExample1.java package com.mkyong.assertions; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class ExceptionExample1 { @Test void test_exception() { Exception exception = assertThrows( ArithmeticException.class, () -> divide(1, 0)); assertEquals("/ …

Read more

TestNG – Expected Exception Test

In this tutorial, we will show you how to use the TestNG expectedExceptions to test the expected exception throws in your code. 1. Runtime Exception This example shows you how to test a runtime exception. If the method divisionWithException () throws a runtime exception – ArithmeticException, it will be passed. TestRuntime.java package com.mkyong.testng.examples.exception; import org.testng.annotations.Test; …

Read more