34

似乎这个问题像世界一样古老,但我仍然找不到解决方案..

我正在尝试运行简单的测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/applicationContext.xml", "/PersonsPopulateTest-context.xml"})
@Transactional
public class PersonsPopulateTest {

文件位于:

src
   main
      resources
           applicationContext.xml

src        
   test
      resources
          PersonsPopulateTest-context.xml 

因此,在构建这些文件之后,它们位于target/classestarget/test-classes

但是 mvn test 命令仍然说: Failed to load ApplicationContext

官方文档怎么说:

@RunWith(SpringJUnit4ClassRunner.class)
// ApplicationContext will be loaded from "/applicationContext.xml" and "/applicationContext-test.xml"
// in the root of the classpath
@ContextConfiguration(locations={"/applicationContext.xml", "/applicationContext-test.xml"})
public class MyTest {
    // class body...
}

我哪里做错了?

谢谢,弗莱基米尔

更新。万无一失的报告:

java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
Caused by: java.lang.IllegalArgumentException: Can not load an ApplicationContext with a NULL 'contextLoader'. Consider annotating your test class with @ContextConfiguration.
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:117)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148)
... 30 more
4

10 回答 10

11

我认为 Maven 根本没有包含来自 main/resources 的 XML 文件。

您可以尝试明确指定要包含在 pom.xml 中的内容。

让我知道以下配置是否有效:

    <!-- Add this directly after the <build>-opening tag -->
    <resources>
        <resource>
            <filtering>true</filtering>
            <directory>src/test/resources</directory>
            <includes>
                <include>**/*.properties</include>
            </includes>
            <excludes>
                <exclude>**/*local.properties</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>

这是我在你的情况下使用的东西。如果您没有要包含的属性文件,您可以编辑它。

于 2012-04-12T09:57:25.460 回答
9

我的测试上下文文件位于src\test\resources\spring文件夹下。我设法加载上下文

@ContextConfiguration(locations={"classpath:**/test-context.xml"})

但是(在 test-context.xml 中)对src\main\resources\spring文件夹下的 application-context.xml 的引用失败

我设法通过在测试类中创建一个 ClassPathXmlApplicationContext 来加载应用程序上下文

ClassPathXmlApplicationContext appContext=new ClassPathXmlApplicationContext(new String[]{"classpath:spring/application-context.xml","classpath:spring/model-context.xml"});

让我知道这是否有帮助或可能会产生任何其他问题。

于 2012-04-12T12:08:16.637 回答
3

您的应用程序上下文必须包含在类路径中并放入 * :

@ContextConfiguration(locations = { "classpath:*/application-context.xml" })
于 2014-05-14T08:56:52.517 回答
3

我认为最佳实践是将用于测试 PersonsPopulateTest-context.xml 的应用程序上下文文件放在 src/test/resources 下。该文件将被复制到目标/测试类中,您可以在测试类中引用它,如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:**/PersonsPopulateTest-context.xml"})
@Transactional
public class PersonsPopulateTest {

}

如果你仍然想引用 src/main/resources 下的 applicationContext.xml,那么你必须包含它如下:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:src/main/resources/applicationContext.xml"})
@Transactional
public class PersonsPopulateTest {

}

它对我有用。

于 2015-03-05T11:38:04.670 回答
2
<!-- Add this directly after the <build>-opening tag in the your pom-->
            <testResources>
                <testResource>
                    <directory>src/test/resources</directory>
                    <filtering>true</filtering>
                    <includes>
                        <include>**/*.xml</include>
                        <include>**/*.properties</include>
                    </includes>
                </testResource>
            </testResources>
于 2013-06-19T20:36:48.613 回答
1

出于某种原因,我遇到了同样的问题,当我使用 JDK6 而不是 JDK8 运行 maven 测试时它起作用了(在我的情况下,这是一个遗留应用程序)

如果它帮助任何人。

于 2015-05-29T22:32:04.093 回答
1

我遇到了同样的问题。对我来说,测试通过 eclipse 成功运行。但是,当我运行时mvn test,它给了我错误:Failed to load ApplicationContext

修复:将 src/test/resources 目录添加到 surefire 插件的类路径中 -

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.10</version>
            <configuration>
                <additionalClasspathElements>
                    <additionalClasspathElement>${project.basedir}/src/test/resources</additionalClasspathElement>
                </additionalClasspathElements>      
            </configuration>

将类路径添加到 SureFire

希望能帮助到你。

于 2015-08-11T01:14:48.713 回答
0

我有同样的问题,所有文件都成功复制到target/classestarget/test-classes,仍然spring找不到它们。

当我在pom中明确指定maven-surefire-pluginmaven-resources-plugin的版本时,问题消失了

于 2013-09-12T08:44:48.720 回答
0

更新:实际上在我的情况下,问题在于Compile on Save启用了选项。我使用 Netbeans,选项设置为For test execution only. 此值编译更改的文件并用新文件替换资源。但是,由于额外使用应用程序资源来测试资源,会在文件夹For test execution only中生成错误生成的资源。target

改变Compile on Save=For test execution only

解决Compile on Save=Disable 问题。

下面的文字也是正确的,但有时不起作用。直到我重新启动 Netbeans IDE 才有效。但是,问题原因的详细信息是正确的,因此,我宁愿离开测试。


OLD:在我的情况下,我appContext-test.xmlsrc/main/resources。当我更改任何代码并启动一个单元测试(不是全部)时,它会重新编译并正确执行。但是,如果我再次启动相同的单元测试,它会因java.lang.IllegalStateException: Failed to load ApplicationContext.

我已经pom.xml

<build>
    <resources>
        <resource>
            <directory>${project.basedir}/src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <testResources>
        <testResource>
            <directory>${project.basedir}/src/test/java/resources</directory>
            <filtering>true</filtering>
        </testResource>
    </testResources>
</build>

<build>
    <resources>
        <resource>
            <directory>${project.basedir}/src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <testResources>
        <testResource>
            <directory>${project.basedir}/src/main/resources</directory>
            <filtering>true</filtering>
        </testResource>
        <testResource>
            <directory>${project.basedir}/src/test/java/resources</directory>
            <filtering>true</filtering>
        </testResource>
    </testResources>
</build>

现在一切正常。

错误是由于使用appContext-test.xml具有src/main/resources/my.properties很多变量的文件,例如

database.url = ${database.url}
database.username = ${database.username}
database.password = ${database.password}

在构建过程中填充。但是,如果您跳过src/main/resources, testResourcethen将按原样my.properties添加, ig 没有替换。target/classes/my.properties这个文件当然打破了上下文。

PS:您可以删除${project.basedir}/-这是我的自定义内容。

于 2014-04-04T15:50:46.080 回答
0

在您的集成测试类中,尝试添加

@Test
public void contextLoads(){
}
于 2021-06-14T09:53:45.137 回答