From the course: Programming Foundations: Test-Driven Development
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Testing exceptions - Java Tutorial
From the course: Programming Foundations: Test-Driven Development
Testing exceptions
- [Instructor] If you have a method that should throw an exception under certain conditions, then you need to test whether it does so when those conditions arise. This requires designing your test cases a little differently, this feature is available in JUnit 4 as well, but the approach is a little different between the two. Let us first see JUnit 4 approach. In JUnit 4 the @Test annotation supports an optional parameter, expected, the declares the name of the exception class that should be thrown in the test case. So if you expect IllegalArgumentException to be thrown, then you say expected= IllegalArgumentException.class. You create those conditions for the exception, and then you invoke the method that should throw the exception. If the method is behaving correctly under the given conditions, then the test case should pass. If it doesn't, that means either the expected exception did not get thrown or some other…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.