Last active
August 23, 2022 11:32
-
-
Save jeyj0/740b89e403298cab346fbf692087fc87 to your computer and use it in GitHub Desktop.
JUnit5-like assertAll for JUnit4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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