@Override public void visitFixtureAnnotation(T annotation, MethodInfo fixtureMethod) { throw new InvalidSpecException("@%s may not be applied to fixture methods") .withArgs(annotation.annotationType().getSimpleName()); }
@Override public void visitSpecAnnotation(T annotation, SpecInfo spec) { throw new InvalidSpecException("@%s may not be applied to Specs") .withArgs(annotation.annotationType().getSimpleName()); }
@Override public void visitFeatureAnnotation(T annotation, FeatureInfo feature) { throw new InvalidSpecException("@%s may not be applied to feature methods") .withArgs(annotation.annotationType().getSimpleName()); }
@Override public void visitFieldAnnotation(T annotation, FieldInfo field) { throw new InvalidSpecException("@%s may not be applied to fields") .withArgs(annotation.annotationType().getSimpleName()); }
public static void checkIsRunnableSpec(Class<?> clazz) { checkIsSpec(clazz); if (Modifier.isAbstract(clazz.getModifiers())) throw new InvalidSpecException("Specification '%s' is not runnable because it is declared abstract") .withArgs(clazz.getName()); }
/** * Checks if the given class is a Spock specification (according to <tt>isSpec()</tt>), * and throws an <tt>InvalidSpecException</tt> with a detailed explanation if it is not. */ public static void checkIsSpec(Class<?> clazz) { if (isSpec(clazz)) return; if (Specification.class.isAssignableFrom(clazz)) throw new InvalidSpecException( "Specification '%s' was not compiled properly (Spock AST transform was not run); try to do a clean build" ).withArgs(clazz.getName()); throw new InvalidSpecException( "Class '%s' is not a Spock specification (does not extend spock.lang.Specification or a subclass thereof)" ).withArgs(clazz.getName()); }
Throwable checkExceptionThrown(Class<? extends Throwable> exceptionType) { Throwable actual = specificationContext.getThrownException(); if (!Throwable.class.isAssignableFrom(exceptionType)) throw new InvalidSpecException( "Invalid exception condition: '%s' is not a (subclass of) java.lang.Throwable" ).withArgs(exceptionType.getSimpleName()); if (exceptionType.isInstance(actual)) return actual; throw new WrongExceptionThrownError(exceptionType, actual); }
private void checkRequiredInteractionAllowed(IMockInteraction interaction) { if (!verified && interaction.isRequired()) { String mockName = name != null ? name : "unnamed"; throw new InvalidSpecException("Stub '%s' matches the following required interaction:" + "\n\n%s\n\nRemove the cardinality (e.g. '1 *'), or turn the stub into a mock.\n").withArgs(mockName, interaction); } }
private void checkRefersToException(FailsWith failsWith) { if (Throwable.class.isAssignableFrom(failsWith.value())) return; throw new InvalidSpecException("@FailsWith needs to refer to an exception type, " + "but does refer to '%s'").withArgs(failsWith.value().getName()); } }
protected void invalidFieldType(FieldInfo field) { if (field.getType() == Object.class) { throw new InvalidSpecException("@Rule field '%s' does not have a declared type. Please add a type declaration.").withArgs(field.getName()); } throw new InvalidSpecException("The declared type of @Rule field '%s' does not appear to be a rule type.").withArgs(field.getName()); } }
protected void checkIsSharedField(FieldInfo field) { if (!field.isShared()) { throw new InvalidSpecException("@ClassRule fields must be @Shared. Either make '%s' @Shared, or use @Rule.").withArgs(field.getName()); } }
protected void checkIsInstanceField(FieldInfo field) { if (field.isShared() || field.isStatic()) { throw new InvalidSpecException("@Rule fields cannot be @Shared. Either do not make '%s' @Shared, or use @ClassRule.").withArgs(field.getName()); } }