Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide first class support for Groovy context loading in tests [SPR-11631] #16254

Closed
spring-projects-issues opened this issue Mar 30, 2014 · 1 comment
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) in: test Issues in the test module status: duplicate A duplicate of another issue type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Mar 30, 2014

Abhijit Sarkar opened SPR-11631 and commented

There is no support for reading Groovy config files in tests without writing a custom ContextLoader. The default @ContextConfiguration tries to interpret Groovy files as XMLs.
I have created a bare bone ContextLoader for supporting Groovy config. I'm interested in submitting a pull request but see that the CONTRIBUTING.md asks that I create a JIRA first.

GroovyContextConfiguration.java

/**
 * Class-level annotation that is used to determine how to load and configure an
 * ApplicationContext for integration tests. Similar to the standard
 * {@link org.springframework.test.context.ContextConfiguration} but uses
 * {@link GenericGroovyContextLoader}.
 *
 * @author Abhijit Sarkar
 * @see GenericGroovyContextLoader
 */
@ContextConfiguration(loader = GenericGroovyContextLoader.class)
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface GroovyContextConfiguration {

    /**
     * @see ContextConfiguration#locations()
     */
    String[] locations() default {};

    /**
     * @see ContextConfiguration#classes()
     */
    Class<?>[] classes() default {};

    /**
     * @see ContextConfiguration#initializers()
     */
    Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>[] initializers() default {};

    /**
     * @see ContextConfiguration#inheritLocations()
     */
    boolean inheritLocations() default true;

    /**
     * @see ContextConfiguration#inheritInitializers()
     */
    boolean inheritInitializers() default true;

    /**
     * @see ContextConfiguration#name()
     */
    String name() default "";

}

GenericGroovyContextLoader.java

public class GenericGroovyContextLoader extends AbstractGenericContextLoader {
    @Override
    protected BeanDefinitionReader createBeanDefinitionReader(final GenericApplicationContext context) {
        final GroovyBeanDefinitionReader reader = new GroovyBeanDefinitionReader(context);

        final Binding binding = getBinding(reader);

        reader.setBinding(binding);

        return reader;
    }

    protected Binding getBinding(final GroovyBeanDefinitionReader reader) {
        return new Binding();
    }

    @Override
    protected String getResourceSuffix() {
        return ".groovy";
    }
}

Usage

@RunWith(SpringJUnit4ClassRunner)
@GroovyContextConfiguration(locations = ['classpath:service-config.groovy', 'classpath:integ-test-config.groovy',
        'classpath:service-config-scan.groovy'])
abstract class AbstractSpringIntegrationTest {
}

Affects: 4.0.3

Issue Links:

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Mar 30, 2014

Sam Brannen commented

This is a duplicate of #15858.

@spring-projects-issues spring-projects-issues added in: test Issues in the test module type: enhancement A general enhancement in: core Issues in core modules (aop, beans, core, context, expression) status: duplicate A duplicate of another issue labels Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) in: test Issues in the test module status: duplicate A duplicate of another issue type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants