单元测试中的 Spring Context 通配符
我们这里有一个项目设置,它非常广泛地使用 Maven 配置文件。我们使用 Spring,虽然我们主要有基于注释的配置,但仍需要一些 XML 配置文件。
这些 Spring XML 配置文件通过各种不同的配置文件引入,在实际的 Web 应用程序中,它们都放在 WEB-INF/spring 中,并加载了 classpath:spring/spring-*.xml
。这很好用。
问题是单元测试:我想测试各种不同的配置文件,而 Spring 似乎在通配符规范方面存在问题,例如当文件分布在多个目录中时。
我认为最简单的解决方案就是在 @ContextConfiguration 测试注释中指定每个配置文件,但不幸的是,如果缺少一个配置文件,Spring 会抛出异常,并且似乎没有一种方法可以扭转这关。
我认为的另一件事可能是在运行测试之前将所有 spring 配置文件转储到一个文件夹中,但这似乎有点混乱。
我只是想知道是否其他人有过这个问题的经验以及任何解决方法。
We have a project setup here which uses Maven profiles quite extensively. We're using Spring, and although we mostly have an annotation-based configuration there are a few XML configuration files needed.
These Spring XML config files are pulled in with various different profiles, and in the actual web application they're all put in WEB-INF/spring and loaded up with classpath:spring/spring-*.xml
. This works fine.
The problem is unit testing: I want to test a variety of different profiles, and Spring seems to have an issue with a wildcard specification like that when the files are spread over several directories.
The easiest solution I think would just be to specify each config file in the @ContextConfiguration
test annotation, but unfortunately if one is missing Spring throws an exception, and there doesn't seem to be a way of turning this off.
The other thing I thought was potentially dumping all spring config files into one folder before running the tests, but that seems a bit of a kludge.
I was just wondering if anyone else had any experience of this problem and any workarounds.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来 Spring 的家伙有 已经想到了这一点。
您可以使用语法:
这似乎工作正常。
It seems that the Spring guys have thought of this already.
You can use the syntax:
Which seems to work properly.