@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 String describeMismatch(Object arg) { throw new InvalidSpecException("*_ may only appear at the end of an argument list"); } }
private void invalidMockCreation() { throw new InvalidSpecException("Mock objects can only be created inside a spec, and only during the lifetime of a feature (iteration)"); } }
@Override public void visitFieldAnnotation(T annotation, FieldInfo field) { throw new InvalidSpecException("@%s may not be applied to fields") .withArgs(annotation.annotationType().getSimpleName()); }
/** * Used in a then-block to access an expression's value at the time just * before the previous where-block was entered. * * @param expression an arbitrary expression, except that it may not * reference variables defined in the then-block * @param <T> the expression's type * @return the expression's value at the time the previous where-block was * entered */ public <T> T old(T expression) { throw new InvalidSpecException("old() can only be used in a 'then' block"); }
@Override public void visitSpecAnnotation(T annotation, SpecInfo spec) { throw new InvalidSpecException("@%s may not be applied to Specs") .withArgs(annotation.annotationType().getSimpleName()); }
@Override public boolean isSatisfiedBy(Object arg) { throw new InvalidSpecException("*_ may only appear at the end of an argument list"); }
@Override public void visitFeatureAnnotation(T annotation, FeatureInfo feature) { throw new InvalidSpecException("@%s may not be applied to feature methods") .withArgs(annotation.annotationType().getSimpleName()); }
private static int convertCount(Object count, boolean inclusive) { if (!(count instanceof Number)) throw new InvalidSpecException("invocation count must be a number"); int intCount = ((Number)count).intValue(); if (!inclusive) intCount--; if (intCount < 0) throw new InvalidSpecException("invocation count must be >= 0"); return intCount; } }
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); } }
/** * Specifies that the preceding <tt>when</tt> block should throw an exception * of the given type. May only occur in a <tt>then</tt> block. * <p>This form of exception condition is typically used if the thrown * exception instance is <em>not</em> used in subsequent conditions. * * <p>Example: * <pre> * when: * "".charAt(0) * * then: * thrown(IndexOutOfBoundsException) * * @param type the expected exception type * @param <T> the expected exception type * @return the thrown exception instance */ public <T extends Throwable> T thrown(Class<T> type) { throw new InvalidSpecException( "Exception conditions are only allowed in 'then' blocks, and may not be nested inside other elements"); }
/** * 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()); }
/** * Specifies that the preceding <tt>when</tt> block should throw an exception. * May only occur as the initializer expression of a typed variable declaration * in a <tt>then</tt> block; the expected exception type is inferred from the * variable type. * <p>This form of exception condition is typically used if the thrown * exception instance is used in subsequent conditions. * * <p>Example: * <pre> * when: * "".charAt(0) * * then: * IndexOutOfBoundsException e = thrown() * e.message.contains(...) * </pre> * * @return the thrown exception instance */ public <T extends Throwable> T thrown() { throw new InvalidSpecException( "Exception conditions are only allowed in 'then' blocks, and may not be nested inside other elements"); }
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()); }
throw new InvalidSpecException("that() can only be used where a condition is expected");
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()); } }
Throwable thrownImpl(String inferredName, Class<? extends Throwable> inferredType) { if (inferredType == null) { throw new InvalidSpecException("Thrown exception type cannot be inferred automatically. " + "Please specify a type explicitly (e.g. 'thrown(MyException)')."); } return checkExceptionThrown(inferredType); }
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 Object createMockImpl(String inferredName, Class<?> inferredType, Object instance, MockNature nature, MockImplementation implementation, Map<String, Object> options, Class<?> specifiedType, Closure closure) { Type effectiveType = specifiedType != null ? specifiedType : options.containsKey("type") ? (Type) options.get("type") : inferredType; if (effectiveType == null) { throw new InvalidSpecException("Mock object type cannot be inferred automatically. " + "Please specify a type explicitly (e.g. 'Mock(Person)')."); } return createMock(inferredName, instance, effectiveType, nature, implementation, options, closure); } }
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()); } }