rlegendi
rlegendi

Reputation: 10606

JBehave sub-scenarios?

Is it possible to add multiple given-when-then blocks to a scenario (something like a sub-scenario)?

Here's an example what I have in mind:

A sample story with a collection of scenarios

Narrative:
  As a dev
  In order to do work
  I want multiple sub-scenarios :-)

Scenario: A sample collection scenario

  Given step1...
  When  step1...
  Then  step1...

  Given step2...
  When  step2...
  Then  step2...

As a workaround, I could use multiple scenarios, but that would require rewriting some glue code for initializing structures (before and after methods).

Any hints how could I avoid that? Thanks in advance!

Upvotes: 1

Views: 937

Answers (1)

krokodilko
krokodilko

Reputation: 36087

Yes, you can do it in this way.

Simple example of the story:

A sample story with a collection of scenarios

Narrative:
  As a dev
  In order to do work
  I want multiple sub-scenarios :-)

Scenario: A sample collection scenario

Given step 1
And step 11

When step 1
And step 11

Then step 1
And step 11

Given step 2
When step 2
Then step 2

A java code:

public class Test extends Steps {

    @Given("step 1")
    public void givenStep1() {
        System.out.println("Given Step 1");
    }

    @Given("step 11")
    public void givenStep11() {
        System.out.println("Given Step 11");
    }

    @Given("step 2")
    public void givenStep2() {
        System.out.println("Given Step 2");
    }

    @When("step $step")
    public void when(String step){
        System.out.println("When Step " + step);
    }

    @Then("step $step")
    public void then(String step){
        System.out.println("Then Step " + step);
    }
}

and a test results:

Running story main/resources/test.story
Given Step 1
Given Step 11
When Step 1
When Step 11
Then Step 1
Then Step 11
Given Step 2
When Step 2
Then Step 2



Notice how And keyword is matched in this example.
When And is used under the Given line, it is treaded as Given, if below Then - then it is matched as Then keyword etc.

Upvotes: 1

Related Questions