ametiste
ametiste

Reputation: 385

spring-test, groovy library and qualifier tag incompatibility

Im trying to add some groovy scripts to existing project and I stuck with making integration tests work. I have few beans marked with <qualifier /> tag, that are used for autowiring both in test and in production code.

Right after I add 'org.codehaus.groovy:groovy-all:2.4.0' (tried other versions aswell) to dependencies, without even any groovy usage, my integration tests stop working, with exception:

    SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@5e5f7983] to prepare test instance [com.dph.groovy.vs.springtest.IntegrationTest@299c9fe7]
    java.lang.IllegalStateException: Failed to load ApplicationContext
        at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:94)
        at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:72)
        at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
        at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
        at 
......
    Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unexpected failure during bean definition parsing
    Offending resource: class path resource [spring/app-config.xml]
    Bean 'service'; nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Tag 'qualifier' must have a 'type' attribute
    Offending resource: class path resource [spring/app-config.xml]
    Bean 'service'
        at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70)
        at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
        at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:323)

Runnning project (with jetty 6 if this matters) doesnt cause any issues though, so I assume there's some trick with spring-test union with groovy.

I might just add 'type' to my qualifiers, however it doesnt solve problem, because I have external dependencies with same qualifier tags configuration, besides this attribute is optional as far as I know.

I'd love to find out at least what are roots of this problem.

I created example project to reproduce the described question and will appreciate any ideas: https://github.com/ametiste/groovy-vs-spring-test

Upvotes: 4

Views: 559

Answers (1)

Sam Brannen
Sam Brannen

Reputation: 31288

You have discovered a bug in Spring's testing support.

Fixed in Spring Framework 4.1.6 and 4.2 RC1

I have fixed this bug for Spring Framework 4.1.6 (scheduled for release at the end of March 2015) and 4.2 (scheduled for release in Q3 2015). For further details, please see JIRA issue SPR-12768.

If you wish to try out the fix before the aforementioned releases, consider building against one of the upcoming nightly snapshots.

Temporary Work-around

In the meantime, (for XML configuration files that you are permitted to edit) you can circumvent this bug by explicitly setting the type attribute in the <qualifier> tag to the intended default value which is "org.springframework.beans.factory.annotation.Qualifier". See the following XML config for an example.

<bean id="foo" class="java.lang.String" c:_="bar">
    <qualifier value="foo" type="org.springframework.beans.factory.annotation.Qualifier" />
</bean>

Regards,

Sam (author of the Spring TestContext Framework)

Upvotes: 2

Related Questions