Example #1
0
 private void doVisit(Requires annotation, ISkippable skippable) {
   Closure condition = createCondition(annotation.value());
   Object result = evaluateCondition(condition);
   if (!GroovyRuntimeUtil.isTruthy(result)) {
     skippable.setSkipped(true);
   }
 }
  public Object intercept(
      Object target, Method method, Object[] arguments, IResponseGenerator realMethodInvoker) {
    IMockObject mockObject =
        new MockObject(
            mockConfiguration.getName(),
            mockConfiguration.getExactType(),
            target,
            mockConfiguration.isVerified(),
            false,
            mockConfiguration.getDefaultResponse(),
            specification);

    if (method.getDeclaringClass() == ISpockMockObject.class) {
      return mockObject;
    }

    // sometimes we see an argument wrapped in PojoWrapper
    // example is when GroovyObject.invokeMethod is invoked directly
    Object[] normalizedArgs = GroovyRuntimeUtil.asUnwrappedArgumentArray(arguments);

    if (target instanceof GroovyObject) {
      if (isMethod(method, "getMetaClass")) {
        return mockMetaClass;
      }
      if (isMethod(method, "setProperty", String.class, Object.class)) {
        Throwable throwable = new Throwable();
        StackTraceElement mockCaller = throwable.getStackTrace()[3];
        if (mockCaller.getClassName().equals("org.codehaus.groovy.runtime.ScriptBytecodeAdapter")) {
          // for some reason, runtime dispatches direct property access on mock classes via
          // ScriptBytecodeAdapter
          // delegate to the corresponding setter method
          String methodName =
              GroovyRuntimeUtil.propertyToMethodName("set", (String) normalizedArgs[0]);
          return GroovyRuntimeUtil.invokeMethod(
              target, methodName, GroovyRuntimeUtil.asArgumentArray(normalizedArgs[1]));
        }
      }
    }

    IMockMethod mockMethod = new StaticMockMethod(method, mockConfiguration.getExactType());
    IMockInvocation invocation =
        new MockInvocation(
            mockObject, mockMethod, Arrays.asList(normalizedArgs), realMethodInvoker);
    IMockController mockController = specification.getSpecificationContext().getMockController();

    return mockController.handle(invocation);
  }