assertThrows in JUnit?assertThrows checks if a specific exception is thrown by a block of code during a test. It helps verify error handling.
assertThrows statement in JUnit 5?Use assertThrows(ExceptionClass.class, () -> { /* code that throws */ }); to test if the exception occurs.
assertThrows return and how can it be used?It returns the thrown exception object, allowing you to check its message or properties for more detailed assertions.
assertThrows preferred over try-catch blocks in tests?assertThrows makes tests cleaner and clearer by directly expressing the expected exception without manual try-catch handling.
assertThrows be used to test checked and unchecked exceptions?Yes, it works for both checked exceptions (like IOException) and unchecked exceptions (like NullPointerException).
assertThrows to check for a NullPointerException?The correct syntax is assertThrows(ExceptionClass.class, Executable), so option A is correct.
assertThrows return after catching the exception?assertThrows returns the caught exception object for further checks.
assertThrows in tests?assertThrows helps test exceptions but does not fix bugs automatically.
assertThrows be used to test if no exception is thrown?assertThrows expects an exception; if none is thrown, the test fails.
assertThrows?assertThrows was introduced in JUnit 5 for better exception testing.
assertThrows to test that a method throws an IllegalArgumentException.assertThrows better than a try-catch block in JUnit tests?