-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JUnit] Invoke @BeforeClass before TestRunStarted event
JUnit is running Cucumber so the expectation is that JUnit's `@BeforeClass` and `@AfterClass` methods are invoked respectively before and after all events emitted by Cucumber. However because the `TestRunStarted` event was emitted in the constructor of `Cucumber` it would precede the the invocation of `@BeforeClass`. In older versions of Cucumber sending the `TestSourceRead` events was coupled to reading the test sources. This made it impossible to read the features ahead of time -required to to create the test description tree and fail on lexer errors- and send `TestRunStarted` after `@BeforeClass`. This is no longer case since #1367 and so this problem can be resolved.
- Loading branch information
1 parent
0dd53da
commit 0392d28
Showing
3 changed files
with
125 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
76 changes: 76 additions & 0 deletions
76
junit/src/test/java/cucumber/runtime/junit/InvokeMethodsAroundEventsTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package cucumber.runtime.junit; | ||
|
||
import cucumber.api.CucumberOptions; | ||
import cucumber.api.event.ConcurrentEventListener; | ||
import cucumber.api.event.EventHandler; | ||
import cucumber.api.event.EventPublisher; | ||
import cucumber.api.event.TestRunFinished; | ||
import cucumber.api.event.TestRunStarted; | ||
import cucumber.api.junit.Cucumber; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.notification.RunNotifier; | ||
import org.junit.runners.model.InitializationError; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.hamcrest.collection.IsIterableContainingInOrder.contains; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class InvokeMethodsAroundEventsTest { | ||
|
||
private static final List<String> events = new ArrayList<>(); | ||
|
||
private static EventHandler<TestRunStarted> testRunStartedEventHandler = new EventHandler<TestRunStarted>() { | ||
@Override | ||
public void receive(TestRunStarted event) { | ||
events.add("TestRunStarted"); | ||
} | ||
}; | ||
private static EventHandler<TestRunFinished> testRunFinishedEventHandler = new EventHandler<TestRunFinished>() { | ||
@Override | ||
public void receive(TestRunFinished event) { | ||
events.add("TestRunFinished"); | ||
} | ||
}; | ||
|
||
@AfterClass | ||
public static void afterClass() { | ||
events.clear(); | ||
} | ||
|
||
@Test | ||
public void finds_features_based_on_implicit_package() throws InitializationError { | ||
Cucumber cucumber = new Cucumber(BeforeAfterClass.class); | ||
cucumber.run(new RunNotifier()); | ||
assertThat(events, contains("BeforeClass", "TestRunStarted", "TestRunFinished", "AfterClass")); | ||
} | ||
|
||
@CucumberOptions(plugin = {"cucumber.runtime.junit.InvokeMethodsAroundEventsTest$TestRunStartedFinishedListener"}) | ||
public static class BeforeAfterClass { | ||
|
||
@BeforeClass | ||
public static void beforeClass() { | ||
events.add("BeforeClass"); | ||
|
||
} | ||
|
||
@AfterClass | ||
public static void afterClass() { | ||
events.add("AfterClass"); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") // Used as a plugin by BeforeAfterClass | ||
public static class TestRunStartedFinishedListener implements ConcurrentEventListener { | ||
|
||
@Override | ||
public void setEventPublisher(EventPublisher publisher) { | ||
publisher.registerHandlerFor(TestRunStarted.class, testRunStartedEventHandler); | ||
publisher.registerHandlerFor(TestRunFinished.class, testRunFinishedEventHandler); | ||
} | ||
|
||
} | ||
} |