org.spockframework.runtime.extension.IMethodInvocation Java Examples

The following examples show how to use org.spockframework.runtime.extension.IMethodInvocation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MicronautSpockExtension.java    From micronaut-test with Apache License 2.0 6 votes vote down vote up
@Override
protected void alignMocks(IMethodInvocation context, Object instance) {
    for (MethodInjectionPoint injectedMethod : specDefinition.getInjectedMethods()) {
        final Argument<?>[] args = injectedMethod.getArguments();
        if (args.length == 1) {
            final Optional<FieldInfo> fld = context.getSpec().getFields().stream().filter(f -> f.getName().equals(args[0].getName())).findFirst();
            if (fld.isPresent()) {
                final FieldInfo fieldInfo = fld.get();
                final Object fieldInstance = fieldInfo.readValue(
                        instance
                );
                if (fieldInstance instanceof InterceptedProxy) {
                    Object interceptedTarget = ((InterceptedProxy) fieldInstance).interceptedTarget();
                    if (mockUtil.isMock(interceptedTarget)) {
                        fieldInfo.writeValue(instance, interceptedTarget);
                    }
                }
            }
        }
    }
}
 
Example #2
Source File: MicronautSpockExtension.java    From micronaut-test with Apache License 2.0 5 votes vote down vote up
private TestContext buildContext(IMethodInvocation invocation, Throwable exception) {
    return new TestContext(
        applicationContext,
        Optional.ofNullable(invocation.getSpec()).map(SpecInfo::getReflection).orElse(null),
        Optional.ofNullable(invocation.getFeature()).map(FeatureInfo::getFeatureMethod).map(MethodInfo::getReflection).orElse(null),
        invocation.getInstance(),
        exception);
}
 
Example #3
Source File: MicronautSpockExtension.java    From micronaut-test with Apache License 2.0 5 votes vote down vote up
@Override
protected void resolveTestProperties(IMethodInvocation context, MicronautTest testAnnotation, Map<String, Object> testProperties) {
    Object sharedInstance = context.getSharedInstance();
    if (sharedInstance instanceof TestPropertyProvider) {
        Map<String, String> properties = ((TestPropertyProvider) sharedInstance).getProperties();
        if (CollectionUtils.isNotEmpty(properties)) {
            testProperties.putAll(properties);
        }
    }
}
 
Example #4
Source File: FailsWithMessageExtension.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void intercept(IMethodInvocation invocation) throws Throwable {
    try {
        invocation.proceed();
    } catch (Throwable t) {
        if (!annotation.type().isInstance(t)) {
            throw new WrongExceptionThrownError(annotation.type(), t);
        }
        if (!annotation.message().equals(t.getMessage())) {
            throw new ComparisonFailure("Unexpected message for exception.", annotation.message(), t.getMessage());
        }
        return;
    }

    throw new WrongExceptionThrownError(annotation.type(), null);
}
 
Example #5
Source File: FailsWithMessageExtension.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void intercept(IMethodInvocation invocation) throws Throwable {
    try {
        invocation.proceed();
    } catch (Throwable t) {
        if (!annotation.type().isInstance(t)) {
            throw new WrongExceptionThrownError(annotation.type(), t);
        }
        if (!annotation.message().equals(t.getMessage())) {
            throw new ComparisonFailure("Unexpected message for exception.", annotation.message(), t.getMessage());
        }
        return;
    }

    throw new WrongExceptionThrownError(annotation.type(), null);
}
 
Example #6
Source File: FailsWithMessageExtension.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void intercept(IMethodInvocation invocation) throws Throwable {
    try {
        invocation.proceed();
    } catch (Throwable t) {
        if (!annotation.type().isInstance(t)) {
            throw new WrongExceptionThrownError(annotation.type(), t);
        }
        if (!annotation.message().equals(t.getMessage())) {
            throw new ComparisonFailure("Unexpected message for exception.", annotation.message(), t.getMessage());
        }
        return;
    }

    throw new WrongExceptionThrownError(annotation.type(), null);
}
 
Example #7
Source File: FailsWithMessageExtension.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void intercept(IMethodInvocation invocation) throws Throwable {
    try {
        invocation.proceed();
    } catch (Throwable t) {
        if (!annotation.type().isInstance(t)) {
            throw new WrongExceptionThrownError(annotation.type(), t);
        }
        if (!annotation.message().equals(t.getMessage())) {
            throw new ComparisonFailure("Unexpected message for exception.", annotation.message(), t.getMessage());
        }
        return;
    }

    throw new WrongExceptionThrownError(annotation.type(), null);
}
 
Example #8
Source File: LoggingTestInterceptor.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Override
public void intercept(IMethodInvocation invocation) throws Throwable {
	String testName = invocation.getIteration().getName();
	try {
		logger.writeStart(testName);
		invocation.proceed();
		logger.writeSuccess(testName);
	} catch (Throwable e) { // NOSONAR
		logger.writeFailure(testName, e);
		throw e;
	}
}
 
Example #9
Source File: GuiceyConfigurationHookInterceptor.java    From dropwizard-guicey with MIT License 5 votes vote down vote up
@Override
public void interceptCleanupSpecMethod(final IMethodInvocation invocation) throws Throwable {
    // for case when base test is used without actual application start and so listeners will never be used
    // cleanup state after tests to not affect other tests
    ConfigurationHooksSupport.reset();
    invocation.proceed();
}
 
Example #10
Source File: GuiceyInterceptor.java    From dropwizard-guicey with MIT License 5 votes vote down vote up
@Override
public void interceptSharedInitializerMethod(final IMethodInvocation invocation) throws Throwable {
    hooks.forEach(GuiceyConfigurationHook::register);
    support.before();
    // static fields
    SpecialFieldsSupport.initClients(null, clientFields, support.getClient(), false);
    injector = support.getInjector();
    injectValues(invocation.getSharedInstance(), true);
    invocation.proceed();
}
 
Example #11
Source File: GuiceyInterceptor.java    From dropwizard-guicey with MIT License 5 votes vote down vote up
@Override
public void interceptCleanupSpecMethod(final IMethodInvocation invocation) throws Throwable {
    // just in case to avoid side-effects
    ConfigurationHooksSupport.reset();
    try {
        invocation.proceed();
    } finally {
        support.after();
    }
}
 
Example #12
Source File: UseActivityInterceptor.java    From android-spock with Apache License 2.0 4 votes vote down vote up
@Override public void interceptSetupMethod(IMethodInvocation invocation) throws Throwable {
  fieldInfo.writeValue(invocation.getInstance(), getActivity());
  invocation.proceed();
}
 
Example #13
Source File: UseApplicationInterceptor.java    From android-spock with Apache License 2.0 4 votes vote down vote up
@Override public void interceptSetupMethod(IMethodInvocation invocation) throws Throwable {
  final Application application =
      Instrumentation.newApplication(applicationClass, instrumentation.getTargetContext());
  fieldInfo.writeValue(invocation.getInstance(), application);
  invocation.proceed();
}
 
Example #14
Source File: WithContextInterceptor.java    From android-spock with Apache License 2.0 4 votes vote down vote up
@Override public void interceptSetupMethod(IMethodInvocation invocation) throws Throwable {
  fieldInfo.writeValue(invocation.getInstance(), InstrumentationRegistry.getTargetContext());
  invocation.proceed();
}
 
Example #15
Source File: GuiceyConfigurationHookInterceptor.java    From dropwizard-guicey with MIT License 4 votes vote down vote up
@Override
public void interceptSharedInitializerMethod(final IMethodInvocation invocation) throws Throwable {
    hooks.forEach(GuiceyConfigurationHook::register);
    invocation.proceed();
}
 
Example #16
Source File: GuiceyInterceptor.java    From dropwizard-guicey with MIT License 4 votes vote down vote up
@Override
public void interceptInitializerMethod(final IMethodInvocation invocation) throws Throwable {
    injectValues(invocation.getInstance(), false);
    invocation.proceed();
}