StepFailureDecorator.java

  1. package org.jbehave.core.reporters;

  2. import org.jbehave.core.failures.StepFailed;
  3. import org.jbehave.core.failures.UUIDExceptionWrapper;
  4. import org.jbehave.core.model.OutcomesTable;
  5. import org.jbehave.core.model.Story;

  6. /**
  7.  * <p>
  8.  * When a step fails, the {@link Throwable} that caused the failure is wrapped
  9.  * in a {@link StepFailed} together with the step during which the failure
  10.  * occurred. If such a failure occurs it will throw the {@link StepFailed}
  11.  * after the story is finished.
  12.  * </p>
  13.  *
  14.  * @see StepFailed
  15.  */
  16. public class StepFailureDecorator extends DelegatingStoryReporter {

  17.     private UUIDExceptionWrapper failure;

  18.     public StepFailureDecorator(StoryReporter delegate) {
  19.         super(delegate);
  20.     }

  21.     @Override
  22.     public void afterStory(boolean givenStory) {
  23.         super.afterStory(givenStory);
  24.         if (failure != null) {
  25.             throw failure;
  26.         }
  27.     }

  28.     @Override
  29.     public void beforeStory(Story story, boolean givenStory) {
  30.         failure = null;
  31.         super.beforeStory(story, givenStory);
  32.     }

  33.     @Override
  34.     public void failed(String step, Throwable cause) {
  35.         failure = (UUIDExceptionWrapper) cause;
  36.         super.failed(step, failure);
  37.     }

  38.     @Override
  39.     public void failedOutcomes(String step, OutcomesTable table) {
  40.         failure = new StepFailed(step, table);
  41.         super.failedOutcomes(step, table);
  42.     }
  43. }