7

The scenario is this:

We are using JBehave and Selenium for system, integration and end to end testing. I am checking the results of a calculation on a page with in excess of 20 values to validate. Using Junit Assert the entire test will fail on the first instance of one of the values being incorrect. What I wanted to do was that if an assertion failure is met then the test continues to execute so that I can then collate all of the values that are incorrect in one test run rather than multiple test runs.

To do this I capture the assertions and write out to a log file anything that fails the validation. This has left me with a couple of issues:

1) The log file where I write out the assertions failures do not contain the name of the JBehave Story or Scenario that was being run when the exception occurred.

2) The JBehave Story or Scenario is listed as having 'Passed' and I want it to be listed as 'Failed'.

Is there any way that I can either log the name of the Story and Scenario out to the additional log file OR get the additional logging written to the JBehave log file?

How can I get the Story / Scenario marked as failed?

In the JBehave configuration I have:

configuredEmbedder()
    .embedderControls()
    .doIgnoreFailureInStories(true)
    .doIgnoreFailureInView(false)
    .doVerboseFailures(true)
    .useStoryTimeoutInSecs(appSet.getMaxRunningTime());

and

.useStoryReporterBuilder(
    new StoryReporterBuilder()
    .withDefaultFormats()
    .withViewResources(viewResources)
    .withFormats(Format.HTML, Format.CONSOLE)
    .withFailureTrace(true)
    .withFailureTraceCompression(true)
    .withRelativeDirectory("jbehave/" + appSet.getApplication())
Martin Schröder
  • 4,176
  • 7
  • 47
  • 81
Gazen Ganados
  • 665
  • 1
  • 7
  • 17

1 Answers1

11

Yes, you can create your own StoryReporter:

 public class MyStoryReporter implements org.jbehave.core.reporters.StoryReporter{
     private Log log = ...

     @Override
     public void successful(String step) {
        log.info(">>successStep:" + step);
     }

    @Override
    public void failed(String step, Throwable cause) {
        log.error(">>error:" + step + ", reason:" + cause);
    }

     ...
 }

and register it like this:

.useStoryReporterBuilder(
    new StoryReporterBuilder()
         .withReporters(new MyStoryReporter())
..
plasma147
  • 2,191
  • 21
  • 35
  • Can you please add more info on how do we create loggers. Please explain: private Log log = ... – farheen Mar 14 '14 at 08:29
  • It would depend on which logging framework you are using. If you are using slf4j: `Logger log = LoggerFactory.getLogger(MyStoryReporter.class);`. The main point is that it could be anything in those callback methods- you could do a sysout, write to a database or anything else – plasma147 Mar 14 '14 at 15:41
  • I am getting this error `java: MyStoryReporter is not abstract and does not override abstract method pendingMethods(java.util.List) in org.jbehave.core.reporters.StoryReporter`. Any help? – vkrams Dec 08 '14 at 22:59
  • Sounds like a compilation issue - try implementing that method. The api has probably changed since I answered this – plasma147 Dec 11 '14 at 11:37
  • I've tried the suggested solution (adding extra logging in beforescenario) , but from some reason the method being executed 3 time before each scenario , any idea why ? – Igal Dec 10 '15 at 11:48
  • Extend from `org.jbehave.core.reporters.NullStoryReporter` adapter to override only needed methods and not implement all of them – Mike Jun 02 '16 at 08:41