October 2017
Intermediate to advanced
350 pages
7h 29m
English
Another important Jupiter assertion is assertThrows. This assertion allows to verify if a given exception is raised in a piece of code. To that aim, the method assertThrows accepts two arguments. First, the exception class expected, and second, an executable object (lambda expression), in which the exception is supposed to happen:
package io.github.bonigarcia;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertThrows;import org.junit.jupiter.api.Test;class ExceptionTest { @Test void exceptionTesting() { Throwable exception = assertThrows(IllegalArgumentException.class, () -> { throw new IllegalArgumentException("a message");}); assertEquals("a message", exception.getMessage()); ...Read now
Unlock full access