Spring 集成测试:无法检测默认资源位置
Spring Integration Testing: Could not detect default resource locations
我正在 运行 使用 Maven 的 Failsafe 插件对我的 Spring 启动应用程序进行集成测试。当我创建一个像这样的简单测试时:
@RunWith (SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(App.class)
public class MyTestIT {
@Test
public void test() {
assertTrue (true);
}
}
然后 运行 mvn verify
我在 Spring 应用程序启动之前看到了以下日志条目(例如,甚至在 Spring 启动横幅之前):
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 启动横幅。测试然后 运行 通过而没有任何错误。虽然这些消息以 INFO 日志级别打印并且测试 运行 正常,但我想一切都很好,但我仍然觉得这些消息很烦人。 那么是不是有什么问题我的配置? 我应该担心这些消息吗?
即使没有任何问题,我还是想了解那里发生了什么以及消息的含义。
我的 maven-failsafe-plugin
只是使用默认配置,我的 App.java
只是一个简单的 class 注释 @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 作为休息端点。我有两个配置 classes spring 安全 class pass.
在 main/resources
文件夹中我有一个 application.properties
文件和一个 log4j2.xml。没有 test/resources
文件夹,但是当我只是将 main/resources 文件夹复制到 test/resources
时,上面的日志消息仍然出现。
更新 2:
我刚刚创建了一个小示例应用程序来尝试重现该问题,而且似乎提到的日志输出在我将 log4j2.xml
文件放入资源文件夹后立即开始出现。我试着把它放在 src/main
或 src/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 或资源文件夹的 positioning/assistance 没有任何关系。
我应该担心这些消息吗?
显然完全没有。
但我仍然觉得这些消息很烦人
不知道是否以及如何关闭该检查(当然,您可以通过将 AbstractContextLoader
的日志级别更改为 WARN
来消除它)。
您可以将 AnnotationConfigContextLoader
与 @Configuration.
结合使用
有关详细信息,请参阅 here。
您可以注释您测试 class 使用更具体的加载程序配置并使用 Java 配置
类似于:
@ContextConfiguration(loader=AnnotationConfigContextLoader.class, classes = ScalaTestConfig.class)
其中 ScalaTestConfig class 注释为:
@Configuration
@ComponentScan(basePackages = {"***", "***"})
基于 abhi 的 link。
我正在 运行 使用 Maven 的 Failsafe 插件对我的 Spring 启动应用程序进行集成测试。当我创建一个像这样的简单测试时:
@RunWith (SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(App.class)
public class MyTestIT {
@Test
public void test() {
assertTrue (true);
}
}
然后 运行 mvn verify
我在 Spring 应用程序启动之前看到了以下日志条目(例如,甚至在 Spring 启动横幅之前):
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 启动横幅。测试然后 运行 通过而没有任何错误。虽然这些消息以 INFO 日志级别打印并且测试 运行 正常,但我想一切都很好,但我仍然觉得这些消息很烦人。 那么是不是有什么问题我的配置? 我应该担心这些消息吗?
即使没有任何问题,我还是想了解那里发生了什么以及消息的含义。
我的 maven-failsafe-plugin
只是使用默认配置,我的 App.java
只是一个简单的 class 注释 @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 作为休息端点。我有两个配置 classes spring 安全 class pass.
在 main/resources
文件夹中我有一个 application.properties
文件和一个 log4j2.xml。没有 test/resources
文件夹,但是当我只是将 main/resources 文件夹复制到 test/resources
时,上面的日志消息仍然出现。
更新 2:
我刚刚创建了一个小示例应用程序来尝试重现该问题,而且似乎提到的日志输出在我将 log4j2.xml
文件放入资源文件夹后立即开始出现。我试着把它放在 src/main
或 src/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 或资源文件夹的 positioning/assistance 没有任何关系。
我应该担心这些消息吗?
显然完全没有。
但我仍然觉得这些消息很烦人
不知道是否以及如何关闭该检查(当然,您可以通过将 AbstractContextLoader
的日志级别更改为 WARN
来消除它)。
您可以将 AnnotationConfigContextLoader
与 @Configuration.
有关详细信息,请参阅 here。
您可以注释您测试 class 使用更具体的加载程序配置并使用 Java 配置
类似于:
@ContextConfiguration(loader=AnnotationConfigContextLoader.class, classes = ScalaTestConfig.class)
其中 ScalaTestConfig class 注释为:
@Configuration
@ComponentScan(basePackages = {"***", "***"})
基于 abhi 的 link。