Implementing Fixtures
In the Java Plugin SDK, a fixture is a class that provides before and after hooks for the plugin. Every plugin must implement classes for two fixture interfaces:
SuiteFixture: for hooks to run one time before and after the whole suiteScenarioFixture: for hooks to run before and after every scenario
If you create your plugin project with the VS Code extension, then your project will have stubs for both fixture classes under the fixtures subpackage. You will need to provide logic for their methods if applicable for your plugin.
Example scenario fixture
A previous example showed how to create steps for managing and incrementing a counter object. Those steps had one major problem, though: a tester could call the increment step before initializing the counter, which would cause a failure. To make these steps safe, it would be best if the plugin could create the counter object and initialize it to zero at the start of each scenario. This can be done with a scenario fixture class. For example:
// Like step definitions, fixtures must be annotated as components.
// Spring Boot will automatically find them and use them.
@Component
// Scenario fixtures must implement the `ScenarioFixture` interface.
public class CounterScenarioFixture implements ScenarioFixture {
@Override
public void markScenarioStart(SuiteContext suiteContext, ScenarioContext scenarioContext) {
// This sets the counter to an initial value of zero before every scenario.
// Now, any of the counter steps can be called safely at any time.
// Its logic is practically the same as `ResetCounterStep`.
scenarioContext.getScenarioState().set(CounterConstants.KEY_COUNTER, 0);
}
@Override
public void markScenarioEnd(SuiteContext suiteContext, ScenarioContext scenarioContext) {
// This can remain empty, since nothing needs to be cleaned up.
}
}