Created
May 29, 2016 22:17
-
-
Save mrmanc/5bcbe80549393d1b9ea13c7b3e8974ac to your computer and use it in GitHub Desktop.
JUnit test with multiple anonymous and separately scoped scenarios in one method
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
import org.junit.Rule; | |
import org.junit.rules.ErrorCollector; | |
import static org.assertj.core.api.Assertions.assertThat; | |
public class ReverseTest { | |
@Rule | |
public ErrorCollector collector = new ErrorCollector(); | |
@org.junit.Test | |
public void reversesStrings() { | |
runAll( | |
() -> { | |
String thing = "thing"; | |
assertThat(Reverse.reverse("mnb")).isEqualTo("bnm"); | |
}, | |
() -> { | |
assertThat(Reverse.reverse("cba")).isEqualTo("abc"); | |
}, | |
() -> assertThat(Reverse.reverse("beef")).isEqualTo("feed") | |
); | |
} | |
private void runAll(Runnable... tests) { | |
for (Runnable test : tests) { | |
collector.checkSucceeds( | |
() -> { | |
test.run(); | |
return null; | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment