Skip to content

Instantly share code, notes, and snippets.

@mrmanc
Created May 29, 2016 22:17
Show Gist options
  • Save mrmanc/5bcbe80549393d1b9ea13c7b3e8974ac to your computer and use it in GitHub Desktop.
Save mrmanc/5bcbe80549393d1b9ea13c7b3e8974ac to your computer and use it in GitHub Desktop.
JUnit test with multiple anonymous and separately scoped scenarios in one method
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