jbehave

Jbehave get Story name inside @BeforeStory


I would like to get the Story name in a method annotated with @BeforeStory.

I need this for debugging purposes, cause i'm running a bunch of stories with runStoriesAsPaths and with multiple threads, and I'm trying to log which thread is running which story.

Is there a way to do this?


Solution

  • first you need to create a new StoryReporter (extend that class). In that class you can add actions to be performed before/after story/scenario/step, and you have the story name. example:

    public class NewStoryReporter implements StoryReporter {
    
    private StoryReporter delegate;
    
    public NewStoryReporter(StoryReporter delegate) {
        this.delegate = delegate;
    }
    
    @Override
    public void beforeStory(Story story, boolean givenStory) {
        delegate.beforeStory(story, givenStory);
    }
    
    @Override
    public void beforeScenario(String scenarioTitle) {
        delegate.beforeScenario(scenarioTitle);
    }
    
    @Override
    public void beforeStep(String step) {
        if(step.equals("When after each step")){
            return;
        }
        delegate.beforeStep(step);
    }
    

    then you need to extend StoryReporterBuilder, this creates your NewStoryReporter. example:

    public class NewStoryReporterBuilder extends StoryReporterBuilder {
    
    @Override
    public StoryReporter build(String storyPath) {
        StoryReporter delegate = super.build(storyPath);
        return new NewStoryReporter(delegate);
    }
    
    }
    

    then in your configuration, create an instance of the NewStoryReporterBuilder, and use it in

    Configuration configuration = new YourConfiguration().useStoryReporterBuilder(newStoryReporterBuilder....)