【问题标题】:How to load Spring Application context in Maven tests如何在 Maven 测试中加载 Spring 应用程序上下文
【发布时间】: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

标签: spring maven junit junit4


【解决方案1】:

假设您的applicationContext.xmlsrc/main/resources/appcontext 中,默认情况下Maven 会在WEB-INF/classes/appcontext 中重新分配它。不必为该资源分配新路径。

您应该能够在测试中找到上下文文件:

@ContextConfiguration(locations={"/appcontext/applicationContext.xml"})

您不应该为此需要额外的 Maven 设置。

【讨论】:

  • 我将它们存储在/src/main/resources/appcontext 中,但是如果我以这种方式尝试,则会出现此错误org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [appcontext/applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [appcontext/applicationContext.xml] cannot be opened because it does not exist
  • 但是你的 Maven 插件不是将它们复制到这里:/classes/application_context.检查它们在构建目录中的位置。使用时会发生什么:@ContextConfiguration(locations={"/application_context/applicationContext.xml"})
  • 是的,它正在那里复制它们。在执行测试之前我有这个日志。 [INFO] Using 'ISO-8859-1' encoding to copy filtered resources. [INFO] Copying 3 resources to C:\Users\amaeztu\workspace\System_Maven/src/main/webapp/WEB-INF/classes/application_context 但后来在执行测试时,这个目录也出现java.io.FileNotFoundException 错误。
  • 所以当您将位置指定为 /application_context/applicationContext.xml 时也会得到 FileNotFound?
  • 我认为检查构建目录会很有用。如果可以的话,您的项目是否构建到目标文件夹?当您复制到${basedir}/src/main/webapp/WEB-INF/classes/application_context 时,生成的构建目录中的结构是否完全相同
猜你喜欢
  • 1970-01-01
  • 2021-06-23
  • 2013-02-03
  • 2016-10-31
  • 2018-07-11
  • 1970-01-01
  • 2013-02-08
  • 1970-01-01
  • 2012-10-28
相关资源
最近更新 更多