BeforeOrAfterFailed.java

  1. package org.jbehave.core.failures;

  2. import static java.text.MessageFormat.format;

  3. import java.lang.annotation.Annotation;
  4. import java.lang.reflect.Method;
  5. import java.util.ArrayList;
  6. import java.util.List;

  7. import org.apache.commons.lang3.StringUtils;

  8. /**
  9.  * Thrown when methods, annotated with before or after annotations (story or scenario),
  10.  * fail.
  11.  */
  12. @SuppressWarnings("serial")
  13. public class BeforeOrAfterFailed extends RuntimeException {

  14.     public BeforeOrAfterFailed(Method method, Throwable cause) {
  15.         super(format("Method {0} (annotated with {1} in class {2}) failed: {3}", method.getName(),
  16.                 toAnnotationNames(method.getAnnotations()), method.getDeclaringClass().getName(), cause), cause);
  17.     }

  18.     private static String toAnnotationNames(Annotation[] annotations) {
  19.         List<String> names = new ArrayList<>();
  20.         for (Annotation annotation : annotations) {
  21.             names.add("@" + annotation.annotationType().getSimpleName());
  22.         }
  23.         return StringUtils.join(names, ",");
  24.     }

  25.     public BeforeOrAfterFailed(Throwable cause) {
  26.         super(cause);
  27.     }
  28. }