JUnit λ
![Maven Central](https://camo.githubusercontent.com/42cc72fa3e0c34e5b9fe244b3a1f3406660e719374183266704920db5b5b890c/68747470733a2f2f6d6176656e2d6261646765732e6865726f6b756170702e636f6d2f6d6176656e2d63656e7472616c2f636f6d2e6769746875622e6d6172736368616c6c2f6a756e69742d6c616d6264612f62616467652e737667)
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.