Ask Your Question
4

How can a parametrized scenario in JBehave be executed until the first error is encountered?

asked 2023-07-05 00:29:17 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-07-05 00:52:02 +0000

bukephalos gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-05 00:29:17 +0000

Seen: 13 times

Last updated: Jul 05 '23