From the course: Complete Guide To Java Testing with JUnit 5 & Mockito
Unlock the full course today
Join today to access over 24,900 courses taught by industry experts.
Testing exceptions in JUnit 5
From the course: Complete Guide To Java Testing with JUnit 5 & Mockito
Testing exceptions in JUnit 5
- [Instructor] When testing your code, it's important to consider what happens when your code is faced with unexpected situations. Whether that's invalid inputs or system failures, your application should gracefully handle these, providing meaningful feedback to users or logging necessary information for debugging. JUnit 5 provides a powerful mechanism to verify whether your code throws expected exceptions during testing. This enables you to validate the behavior of exception handling logic. Let's take a look at the calculator class again. Here, if the divisor is zero, we should test that the illegal argument exception is thrown. Currently in our test, we catch the exception and verify the result has not been set. We can make this test a little more robust by leveraging the assert throws method to directly assert that the exception is thrown. This will not only simplify the code, but also provide clear feedback in case of a failure. Let's refactor our test to utilize assert throws. By…
Contents
-
-
-
-
Set up JUnit 5 in your Java application2m 24s
-
(Locked)
Write and run your first JUnit 5 test4m 11s
-
(Locked)
Assertions in JUnit 57m 56s
-
(Locked)
JUnit 5 test lifecycle1m 51s
-
(Locked)
JUnit 5 lifecycle hooks2m 49s
-
(Locked)
Testing exceptions in JUnit 52m 54s
-
(Locked)
Debug tests in JUnit 54m 30s
-
(Locked)
Best practices for writing tests in JUnit 52m 17s
-
(Locked)
Challenge: Write a JUnit 5 test for a temperature converter47s
-
(Locked)
Solution: Write a JUnit 5 Test for a Temperature Converter6m 33s
-
-
-
-
-
-
-
-