Can one Given When Then story drive > 1 JBhave Ste

2019-03-02 12:34发布

问题:

I've created a .story file with a Given When Then (GWT).

Contact_List.story Scenario: Discover Contact Given I've a contact list of friends When one of them is online Then that friend is displayed in a list

I'd like to have two levels of testing (a bunch of fast service layer tests, and a few UI tests). So I created the following using the exact same GWT language:

ServiceSteps.java

@Given("I've a contact list of friends")
...

UISteps.java

@Given("I've a contact list of friends")
....

And Configured JBehave to use both of them: RunBDDTests.java

...
@Override
public InjectableStepsFactory stepsFactory() {       
    // varargs, can have more that one steps classes
    return new InstanceStepsFactory(configuration(), new ServiceSteps(), new UISteps());
}
...

But, when running this in JUNit, each time I run the tests, it's random as to which Steps class it selects.

How to have it run both steps each time so that one .story file drives > 1 steps class?

回答1:

This is organized by the Configuration. In JBehave parlance, the Configuration is the class that tells the JBehave framework how to associate *.stories with *Steps.java. In the questioniers example, this is RunBDDTests.java. One option that will associate two steps with a single GWT scenario is to create two Configurations, one for the Service steps and one for the UI steps:

ServiceConfiguration.java

public class ServiceConfiguration extends JUnitStories
{
 @Override
 public InjectableStepsFactory stepsFactory() {       

    return new InstanceStepsFactory(configuration(), new ServiceSteps()); // <- note steps class
 }

@Override
protected List<String> storyPaths() {

    return new StoryFinder().findPaths(CodeLocations.codeLocationFromClass(this.getClass()), "**/Contact_List.story", "");  //<- note story file name
}
}

UIConfiguration.java

public class UIConfiguration extends JUnitStories
{
    @Override
    public InjectableStepsFactory stepsFactory() {              
      return new InstanceStepsFactory(configuration(), new UISteps()); // <- note steps class
    }

@Override
protected List<String> storyPaths() {       
  return new StoryFinder().findPaths(CodeLocations.codeLocationFromClass(this.getClass()), "**/Contact_List.story", "");  //<- note story file name
}
}

The above two configurations will run two different step files against one .story.



标签: bdd jbehave