@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)"); } }
/** * 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 boolean isSatisfiedBy(Object arg) { throw new InvalidSpecException("*_ may only appear at the end of an argument list"); }
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; } }
@Override public void visitFixtureAnnotation(T annotation, MethodInfo fixtureMethod) { throw new InvalidSpecException("@%s may not be applied to fixture methods") .withArgs(annotation.annotationType().getSimpleName()); }
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); }
@Override public void visitFieldAnnotation(T annotation, FieldInfo field) { throw new InvalidSpecException("@%s may not be applied to fields") .withArgs(annotation.annotationType().getSimpleName()); }
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); } }
@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()); }
static Object createMock(Class<?> mockType, List<Class<?>> additionalInterfaces, List<Object> constructorArgs, IProxyBasedMockInterceptor mockInterceptor, ClassLoader classLoader) { if (constructorArgs != null) { throw new InvalidSpecException("Interface based mocks may not have constructor arguments"); } List<Class<?>> interfaces = new ArrayList<>(); interfaces.add(mockType); interfaces.addAll(additionalInterfaces); interfaces.add(ISpockMockObject.class); return Proxy.newProxyInstance( classLoader, interfaces.toArray(new Class<?>[interfaces.size()]), new DynamicProxyMockInterceptorAdapter(mockInterceptor) ); }
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()); }
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); }
public InteractionBuilder setRangeCount(Object minCount, Object maxCount, boolean inclusive) { this.minCount = minCount instanceof Wildcard ? 0 : convertCount(minCount, true); this.maxCount = maxCount instanceof Wildcard ? Integer.MAX_VALUE : convertCount(maxCount, inclusive); if (this.minCount > this.maxCount) throw new InvalidSpecException("lower bound of invocation count must come before upper bound"); return this; }
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()); } }
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); } }
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()); } }