Skip to content

Instantly share code, notes, and snippets.

@jeyj0
Last active August 23, 2022 11:32
Show Gist options
  • Select an option

  • Save jeyj0/740b89e403298cab346fbf692087fc87 to your computer and use it in GitHub Desktop.

Select an option

Save jeyj0/740b89e403298cab346fbf692087fc87 to your computer and use it in GitHub Desktop.
JUnit5-like assertAll for JUnit4
public class AssertAll {
@Rule
public ErrorCollector collector = new ErrorCollector();
private void assertAll(Runnable... assertions) {
Arrays.stream(assertions).forEach(this::runAndCollectThrowable);
}
private void runAndCollectThrowable(Runnable runnable) {
try {
runnable.run();
} catch (Throwable throwable) {
collector.addError(throwable);
}
}
}
public class TestClass {
@Test
public void shouldTestSomething() {
assertAll(
() -> assertThat(something, is(true)),
() -> assertThat(anotherThing, is(false))
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment