JUnit λ
JUnit extensions built on Java 8 lambdas. Helps to test exceptions and can be used instead of the following pattern:
try {
Long.parseLong("foo");
fail("'foo' should not be a valid long");
} catch (NumberFormatException e) {
// should reach here
}
You can either use #assertRaises
import static com.github.marschall.junitlambda.LambdaAssert.assertRaises;
import org.junit.Test;
public final class JunitLambdaTest {
@Test
public void testNumberFormatException() {
assertRaises(() -> Long.parseLong("foo"), NumberFormatException.class);
}
}
or the Hamcrest matcher #throwsException
import static com.github.marschall.junitlambda.ThrowsException.throwsException;
import org.junit.Test;
public final class JunitLambdaTest {
@Test
public void testNumberFormatException() {
assertThat(() -> Long.parseLong("foo"), throwsException(NumberFormatException.class));
}
}
<dependency>
<groupId>com.github.marschall</groupId>
<artifactId>junit-lambda</artifactId>
<version>0.3.0</version>
<scope>test</scope>
</dependency>
The code is under MIT license.