Spring上下文测试找不到配置位置

Ada*_*m B 8 junit spring spring-test

我有一个大型应用程序分布在多个Spring bean定义xml文件中.在我的测试套件中,我使用FileSystemXmlApplicationContext手动加载我需要的XML文件,以执行我想要运行的测试.这减少了测试设置时间,并允许我使用生产中使用的完全相同的配置文件.

现在我正在尝试使用Spring的事务测试基类,它接受配置位置并为我加载上下文.出于某种原因,当创建应用程序上下文时,Spring无法找到任何配置文件.这很令人困惑,因为我从同一个工作目录运行测试,就像我自己使用FileSystemXmlApplicationContext加载配置一样.如果我在所有配置位置前加上"file:",则会找到我在测试中指定的路径,但找不到配置中定义的bean(例如属性文件)导入或引用的任何文件.这是怎么回事?我是否可以获得扩展spring上下文测试类的测试与我自己创建上下文的测试相同?

例如,创建像这样的上下文工作正常:

ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[] { "WEB-INF/services-context.xml"})
Run Code Online (Sandbox Code Playgroud)

如果我扩展AbstractTransactionalDataSourceSpringContextTests,则以下找不到services-context.xml:

@Override
protected String[] getConfigLocations() {
   return new String[] { "WEB-INF/services-context.xml"};
}
Run Code Online (Sandbox Code Playgroud)

这找到了services-context,但是在那里定义的PropertyPlaceholderConfigurer无法找到它的属性文件.

 @Override
 protected String[] getConfigLocations() {
    return new String[] { "file:WEB-INF/services-context.xml"};
 }
Run Code Online (Sandbox Code Playgroud)

Ada*_*m B 3

除了重写 getConfigLocations 之外,我还重写了 loadContext 并在其中使用了可靠的 fileSystemXmlApplicationContext。

 @Override
 protected String[] getConfigLocations() {
     return new String[] { "WEB-INF/services-config.xml" };
 }

 @Override
 protected ConfigurableApplicationContext loadContext(String[] locations) throws Exception {
     return new FileSystemXmlApplicationContext(locations);
  }
Run Code Online (Sandbox Code Playgroud)