All About xUnit
Testing Exceptions
When the code under test is expected to throw an exception,
the test must include special handling.
Otherwise, the test would terminate abnormally, whereas it should really pass
if the exception is thrown as designed.In most situations, the easy way needs very little code to checks for an exception.Call the code under the test with no special try-catch handling. Instead,
use the test must include special handling.
assertThrows()
with a lambda expression that contains the code that should throw the
exception (() -> { Integer.parseInt(input); })
. If the exception
is not thrown or is of the wrong type, the test will fail.
(Reading...)
Formatting Hints:
To format... | use this markup: |
---|---|
source code | {source:lang=java|lineNumbers=false}...{source} (Supported languages: java, cpp, csharp, c, python.) |
bold text | __some text__ |
italicized text | ~~some text~~ |
underlined text | @@some text@@ |
a method or class name | ``myMethod(...)`` |