January 2018
Intermediate to advanced
434 pages
14h 1m
English
In this recipe, we will be using ExpectedException as the JUnit rule because it helps the test declare that an exception is expected and also provides a way to clearly express the expected behavior. It is much more flexible than using the @Test(expected= ...) annotation because we can test specific error messages and custom fields.
In the following steps, we will learn how to write JUnit tests:
fun methodThrowsException() { throw IllegalArgumentException("Age must be integer")}
@Rulevar thrown = ExpectedException.none()@Testfun testExceptionFlow() ...
Read now
Unlock full access