Spring上下文测试找不到配置位置
我有一个大型应用程序分布在多个 Spring bean 定义 xml 文件中。 在我的测试套件中,我使用 FileSystemXmlApplicationContext 手动加载所需的 XML 文件来执行我想要运行的测试。 这减少了测试设置时间,并允许我使用与生产中使用的完全相同的配置文件。
现在我尝试使用 Spring 的事务测试基类,它获取配置位置并为我加载上下文。 由于某种原因,当创建应用程序上下文时,Spring 找不到任何配置文件。 这很令人困惑,因为我从与使用 FileSystemXmlApplicationContext 自己加载配置时相同的工作目录运行测试。 如果我在所有配置位置前面加上“file:”,则会找到我在测试中指定的路径,但无法找到配置中定义的 bean 导入或引用的任何文件(例如属性文件)。 这是怎么回事? 我可以获得扩展 spring 上下文测试类的测试,使其与我自己创建上下文的测试类的工作方式相同吗?
例如,创建这样的上下文效果很好:
ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[] { "WEB-INF/services-context.xml"})
如果我扩展 AbstractTransactionalDataSourceSpringContextTests,则以下内容找不到 services-context.xml:
@Override
protected String[] getConfigLocations() {
return new String[] { "WEB-INF/services-context.xml"};
}
这会找到 services-context,但其中定义的 PropertyPlaceholderConfigurer 无法找到它的属性文件。
@Override
protected String[] getConfigLocations() {
return new String[] { "file:WEB-INF/services-context.xml"};
}
I have a large application spread across multiple Spring bean definition xml files. In my test suite I manually load up the XML files I need using a FileSystemXmlApplicationContext to perform the tests I want to run. This reduces test set up time and allows me to use the same exact configuration files that are used in production.
Now I'm trying to use Spring's transactional test base classes which take the config locations and load the context for me. For some reason when the application context is created Spring cannot find any of the config files. This is confusing because I run the test from the same working directory as when I load the config myself using FileSystemXmlApplicationContext. If I prepend all my config locations with "file:" the paths I specify in my test are found, but any files that are imported or referenced by beans defined in the config (e.g. properties files) cannot be found. What's the deal? Can I get tests that extend the spring context test classes to work the same as the ones where I create the context myself?
For example, creating the context like this works fine:
ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[] { "WEB-INF/services-context.xml"})
If I extend AbstractTransactionalDataSourceSpringContextTests the following does not find services-context.xml:
@Override
protected String[] getConfigLocations() {
return new String[] { "WEB-INF/services-context.xml"};
}
This finds services-context, but the PropertyPlaceholderConfigurer defined in there fails to find it's properties files.
@Override
protected String[] getConfigLocations() {
return new String[] { "file:WEB-INF/services-context.xml"};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我们将所有 Spring 配置和属性文件放在类路径中,这使事情变得简单 - 我们可以从基类扩展我们的测试类,如下所示:
这里,路径都是类路径中的路径。
如果您不想这样做,您是否检查过如何引用 services-context.xml 中的属性文件? 我怀疑如果您将 file: 添加到上下文配置中,那么您还需要将其添加到属性文件引用中。 您也许可以使用单独的测试 Spring 配置文件来更改属性占位符的定义,并将其放置在上下文文件列表的末尾 - 然后它的定义将覆盖早期文件中定义的定义。
We put all of our Spring config and properties files in the classpath, which keeps things simple - we can just extend our test classes from a base class like:
Here, the paths are all paths in classpath.
If you don't want to do that, have you checked how you are referencing the properties files in your services-context.xml? I suspect that if you add file: to your context configuration, then you'll also need to add this to your property file reference. You could perhaps just use a separate test Spring config file to change the definition of your property placeholder, and place this at the end of your list of context files - its definitions will then override those defined in earlier files.
除了重写 getConfigLocations 之外,我还重写了 loadContext 并在其中使用了可靠的 fileSystemXmlApplicationContext。
In addition to overriding getConfigLocations I also overrode loadContext and used a trusty fileSystemXmlApplicationContext in there.
您的配置位置是相对 URI,并且将由基测试类进行解释,并且 URI 相对于测试类本身的位置进行解析。 尝试使用完全限定的 URI,或使用相对 URI,同时考虑测试类的位置。
Your config locations are relative URIs, and will be interpreted as such by the base test class, with the URI being resolved relative to the location of the test class itself. Try using fully qualified URIs, or use relative URI taking into account where the test class is.
你不能使用像 ClassPathXmlApplicationContext?
Can't you use classpath XML factories like ClassPathXmlApplicationContext?
另一种可能的解决方案是复制
services-config.xml
并重命名为services-config-test.xml
,然后放在类路径下。 属性文件也是如此。Another possible solution is to duplicate the
services-config.xml
and rename asservices-config-test.xml
and then put under classpath. The same thing goes for properties file.