【发布时间】:2013-01-17 09:30:48
【问题描述】:
我有一个 Maven 网络项目,我想在其中执行一些 JUnit 集成测试。为此,我想加载 applicationContext.xml 文件以获取 Spring 注入的 bean。我的应用程序上下文文件也位于src/main/resources/app_context,但在执行 Maven 的生命周期时,它会将它们定位到WEB-INF/classes/application_context,因此我可以通过类路径访问它们。
<resource>
<directory>src/main/resources/appcontext</directory>
<targetPath>${basedir}/src/main/webapp/WEB-INF/classes/application_context</targetPath>
<filtering>true</filtering>
<includes>
<include>*/**</include>
</includes>
</resource>
我尝试以某种方式访问该上下文,但没有成功。我也以这种方式将它们放入 Maven 的目标测试目录中:
<testResources>
<testResource>
<directory>
src/main/resources/appcontext
</directory>
<targetPath>${basedir}/src/main/webapp/WEB-INF/classes/application_context</targetPath>
</testResource>
</testResources>
这是我测试的一部分:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:**/applicationContext.xml" })
@Transactional
public class SystemTest {
@Autowired
ApplicationContext applicationContext;
@Test
public void initializeSystemTest() {
//Test code
这就是我如何从我的主要文件访问应用程序上下文文件的其余部分:
<?xml version="1.0" encoding="ISO-8859-15"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cxf="http://cxf.apache.org/core" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<import resource="classpath:application_context/ApplicationContext*.xml" />
<context:annotation-config />
<!--========== Spring Data Source ========== -->
<!--Bean stuff-->
基本上,我只想将每个 applicationContext 文件的副本存储在src/main/resources 中,并让 Maven 复制它们以用于测试和执行范围。但是我的测试类无法找到并加载它们。我该怎么办?
【问题讨论】:
-
你为什么要从
appcontext复制到application_context?那只会混淆问题。为什么不直接使用默认值,在这种情况下src/main/resources/appcontext会以WEB-INF/classes/appcontext结束?在您的 SCM 上也将更容易,因为您不会在构建时更改src。