gpt4 book ai didi

java - Spring 集成测试 : Could not detect default resource locations

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:41:33 24 4
gpt4 key购买 nike

我正在使用 Maven 的 Failsafe 插件为我的 Spring Boot 应用程序运行集成测试。当我创建一个像这样的简单测试时:

@RunWith (SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(App.class)
public class MyTestIT {

@Test
public void test() {
assertTrue (true);
}
}

然后运行 ​​mvn verify 我在 Spring 应用程序启动之前(例如,甚至在 Spring Boot 横幅之前)看到以下日志条目:

Running org.....MyTestIT
2016-04-14 13:25:01.166 INFO ???? --- [ main]
or.sp.te.co.su.AbstractContextLoader :
Could not detect default resource locations for test class
[org....MyTestIT]: no resource found for suffixes
{-context.xml, Context.groovy}.
2016-04-14 13:25:01.175 INFO ???? --- [ main]
or.sp.te.co.su.DefaultTestContextBootstrapper :
Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
2016-04-14 13:25:01.185 INFO ???? --- [ main]
or.sp.te.co.su.DefaultTestContextBootstrapper : Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@57c758ac, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@a9cd3b1, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@13e39c73, org.springframework.test.context.support.DirtiesContextTestExecutionListener@64cd705f, org.springframework.test.context.transaction.TransactionalTestExecutionListener@9225652, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@654f0d9c]

后面是 Spring Boot 横幅。测试然后运行,没有任何错误。虽然这些消息是用 INFO 日志级别打印的,并且测试运行正常,但我想一切都很好,但我仍然觉得这些消息很烦人。那么我的配置有问题吗? 我应该担心这些消息吗?

即使没有任何问题,我仍然想了解那里发生了什么以及消息的含义。

我的 maven-failsafe-plugin 只是使用默认配置,而我的 App.java 只是一个用 @SpringBootApplication 注释的简单类.

更新 1:

这是我的测试插件的配置:

  <plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- Ignore any tests that are marked by the @IntegrationTest annotation of Spring Boot -->
<excludedGroups>org.springframework.boot.test.IntegrationTest</excludedGroups>
</configuration>
</plugin>

<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>

我还配置了一个 spring-boot-starter-test 依赖项,我正在使用 spring-boot-devtools。除此之外,其他一切都与测试无关。

它自己的项目非常标准,使用 hibernate 和 mysql 以及 web-mvc 作为 rest 端点。我在类(class)通行证上有两个用于 spring security 的配置类。

main/resources 文件夹中,我有一个 application.properties 文件和一个 log4j2.xml。没有 test/resources 文件夹,但是当我只是将 main/resources 文件夹复制到 test/resources 时,上面的日志消息仍然出现。

更新 2:我刚刚创建了一个小的示例应用程序来尝试重现该问题,并且当我将 log4j2.xml 文件放入资源文件夹时,似乎提到的日志输出开始出现。我尝试将它放在 src/mainsrc/test 或两者中,效果相同。

最佳答案

这是运行 Spring 集成测试时的默认行为。

来自引用文档

If you omit both the locations and value attributes from the @ContextConfiguration annotation, the TestContext framework will attempt to detect a default XML resource location. Specifically, GenericXmlContextLoader and GenericXmlWebContextLoader detect a default location based on the name of the test class. If your class is named com.example.MyTest, GenericXmlContextLoader loads your application context from "classpath:com/example/MyTest-context.xml".

因此,它与 Maven、log4j 或资源文件夹的定位/辅助无关。

我应该担心这些消息吗?

显然,一点也不。

但我仍然觉得这些消息很烦人

不知道是否以及如何关闭该检查(当然,您可以通过将 AbstractContextLoader 的日志级别更改为 WARN 来消除它)。

关于java - Spring 集成测试 : Could not detect default resource locations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36622871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com