Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
student:junit:v5 [2020/02/23 10:44] – Fixed example of assertThrows() bernstdh | student:junit:v5 [2021/01/19 11:20] – bernstdh | ||
---|---|---|---|
Line 147: | Line 147: | ||
- | There are several ways to test for thrown exceptions in JUnit. The most common is to use the '' | + | There are several ways to test for thrown exceptions in JUnit. The most common is to use the '' |
+ | === Testing for exceptions === | ||
- | <code java> | + | One can invoke the methods that is supposed to throw an expression in a '' |
- | /** | + | |
- | * Test that the constructor validates properly. | + | |
- | */ | + | |
- | @Test | + | |
- | public void constructor_IllegalArguments() | + | |
- | { | + | |
- | assertThrows(IllegalArgumentException.class, | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | This approach uses a //Lambda expression//, | + | |
- | (Note that '' | + | |
- | + | ||
- | + | ||
- | Alternatively, | + | |
<code java> | <code java> | ||
Line 188: | Line 174: | ||
</ | </ | ||
+ | === Testing for exceptions canonically (with lambda expressions) === | ||
+ | Suppose the constructor of the '' | ||
+ | |||
+ | <code java> | ||
+ | /** | ||
+ | * Test that the constructor validates properly. | ||
+ | */ | ||
+ | @Test | ||
+ | public void constructor_IllegalArguments() | ||
+ | { | ||
+ | assertThrows(IllegalArgumentException.class, | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | This approach uses a //Lambda expression//, | ||
==== Other Useful Methods in the Assert Class ==== | ==== Other Useful Methods in the Assert Class ==== | ||