Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In order to execute a parametrized scenario in JBehave until the first error is encountered, you can use the JUnit test framework. You can create a JUnit test case that runs the scenario, and use the JUnit annotations to configure the test execution.

Here's an example of how to do this:

@RunWith(Parameterized.class)
public class MyScenarioTest {

    private String input;

    public MyScenarioTest(String input) {
        this.input = input;
    }

    @Parameters(name = "{index}: input={0}")
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {
                { "input 1" },
                { "input 2" },
                { "input 3" },
                { "input 4" }
        });
    }

    @Test
    public void runScenario() throws Throwable {
        List<String> storyPaths = Arrays.asList("path/to/story.story");
        Configuration configuration = new MostUsefulConfiguration();
        StoryLoader storyLoader = new LoadFromClasspath();
        StoryReporterBuilder reporterBuilder = new StoryReporterBuilder()
                .withFormats(CONSOLE, TXT, HTML, XML)
                .withDefaultFormats();
        StoryRunner storyRunner = new StoryRunner();
        StoryControls storyControls = new StoryControls();
        storyControls.doSkipScenariosAfterFailure(true);
        storyControls.doResetStateBeforeScenario(false);
        StoryExecutionResult result = storyRunner.run(storyPaths, configuration, storyLoader, reporterBuilder.build(), storyControls);
        if (result.isFailure()) {
            Throwable cause = result.getFailure().getCause();
            if (cause != null) {
                throw cause;
            } else {
                throw result.getFailure();
            }
        }
    }
}

In this example, the MyScenarioTest class is annotated with the JUnit @RunWith(Parameterized.class) annotation, which means that the test case will be run multiple times with different input parameters. The input parameters are defined by the data() method, which returns a collection of arrays containing the input values.

The runScenario() method is the test method that runs the scenario. This method uses the JBehave API to load and execute the story, and checks the result for failures. If a failure occurs, the doSkipScenariosAfterFailure(true) method call ensures that subsequent scenarios are skipped, until the test case is complete.

By using this approach, you can run your parametrized scenario multiple times with different input values, and ensure that the test stops as soon as the first failure occurs.