Skip to content

Commit 315e6db

Browse files
committed
Invoke (Before|After)Class and TestRules around Cucumber execution
Junit invokes several framework methods around the test execution. The cucumber runner should finish its whole execution inside these. To be precises these two should be equivalent: ``` @BeforeClass public static void before { // Do stuff } @test public void test(){ cucumber.api.cli.Main.main(some arguments....) } @afterclass public static void after { // Do stuff } ``` ``` @RunWith(Cucumber.class) public class Test { @BeforeClass public static void before { // Do stuff } @afterclass public static void after { // Do stuff } } ``` By firing the TestRunFinished event and printing the summary directly after invoking the children rather then after the completion of the whole test run these are made equivalent again.
1 parent ec8770d commit 315e6db

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

junit/src/main/java/cucumber/api/junit/Cucumber.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.junit.runner.notification.RunNotifier;
2020
import org.junit.runners.ParentRunner;
2121
import org.junit.runners.model.InitializationError;
22+
import org.junit.runners.model.Statement;
2223

2324
import java.io.IOException;
2425
import java.util.ArrayList;
@@ -75,7 +76,7 @@ public Cucumber(Class clazz) throws InitializationError, IOException {
7576
* @param runtimeOptions configuration
7677
* @return a new runtime
7778
* @throws InitializationError if a JUnit error occurred
78-
* @throws IOException if a class or resource could not be loaded
79+
* @throws IOException if a class or resource could not be loaded
7980
*/
8081
protected Runtime createRuntime(ResourceLoader resourceLoader, ClassLoader classLoader,
8182
RuntimeOptions runtimeOptions) throws InitializationError, IOException {
@@ -99,10 +100,16 @@ protected void runChild(FeatureRunner child, RunNotifier notifier) {
99100
}
100101

101102
@Override
102-
public void run(RunNotifier notifier) {
103-
super.run(notifier);
104-
runtime.getEventBus().send(new TestRunFinished(runtime.getEventBus().getTime()));
105-
runtime.printSummary();
103+
protected Statement childrenInvoker(RunNotifier notifier) {
104+
final Statement features = super.childrenInvoker(notifier);
105+
return new Statement() {
106+
@Override
107+
public void evaluate() throws Throwable {
108+
features.evaluate();
109+
runtime.getEventBus().send(new TestRunFinished(runtime.getEventBus().getTime()));
110+
runtime.printSummary();
111+
}
112+
};
106113
}
107114

108115
private void addChildren(List<CucumberFeature> cucumberFeatures) throws InitializationError {

0 commit comments

Comments
 (0)