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

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues 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:

  • #15858 Support Groovy scripts for bean definitions in the TestContext framework ("duplicates")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants