如何使用Roo集成测试来使用AnnotationConfigContextLoader

时间:2014-06-05 22:05:47

标签: java spring spring-roo

我使用基于XML的弹簧配置和弹簧3.1运行roo生成的集成测试已有一段时间了。但是,我最近从基于XML的配置更改为基于java的配置,并且还从Spring 3.1升级到Spring 4.0。

不幸的是,我的roo Genenerated集成测试现在失败了,例外:

java.lang.IllegalStateException: Test class [com.bulb.learn.domain.CounterIntegrationTest] has been configured with @ContextConfiguration's 'locations' (or 'value') attribute {classpath*:/META-INF/spring/applicationContext*.xml}, but AnnotationConfigContextLoader does not support resource locations.
    at org.springframework.test.context.support.AnnotationConfigContextLoader.validateMergedContextConfiguration(AnnotationConfigContextLoader.java:164)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:111)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)
    ... 32 more

我知道这是由roo生成的方面文件中的这一行引起的:

declare @type: CounterIntegrationTest: @ContextConfiguration(locations = "classpath*:/META-INF/spring/applicationContext*.xml");

那是因为我将它用于我的主要测试类基类:

@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles(profiles = "junit")
@ContextConfiguration(classes = { CoreConfig.class }, loader = AnnotationConfigContextLoader.class)
public abstract class CoreIntegrationTest {
    ... 
}

以下是我如何定义由@RooIntegrationTest

注释的类
@RooIntegrationTest(entity = Counter.class, transactional = false)
public class CounterIntegrationTest extends CoreIntegrationTest {

但是,我不确定如何修复它。

roo测试过去运行良好。只需升级到Spring 4.0,它们也运行良好。只需更改为Spring 3.1的java配置,它们也可以正常运行。但是,他们不能同时使用(Spring 4.0 with Java config)

有没有人有任何想法如何让这些工作无需简单地从roo控件中删除它们并自行删除违规行?

提前致谢!

1 个答案:

答案 0 :(得分:0)

事实证明,Roo会在注释类中查看它是否已经有一个@ContextConfiguration,然后再添加一个方面。不幸的是,它没有检查它是否继承了@ContextConfiguration

我能够通过向带注释的类添加默认@ContextConfiguration来解决此问题,如下所示:

@ContextConfiguration
@RooIntegrationTest(entity = Counter.class, transactional = false)
public class CounterIntegrationTest extends CoreIntegrationTest {

这可以防止Roo生成自己的,因此错误就会消失。