Spring Integration Testing:无法检测默认资源位置

新手上路,请多包涵

我正在使用 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 作为休息端点。我在类传递上有两个用于 spring 安全的配置类。

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

更新 2: 我刚刚创建了一个小型示例应用程序,试图重现该问题,而且似乎提到的日志输出在我将 log4j2.xml 文件放入资源文件夹后立即开始出现。我试着把它放在 src/mainsrc/test 或两者都具有相同的效果。

原文由 lanoxx 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 870
2 个回答

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

来自参考文档

如果您从 @ContextConfiguration 注释中省略了 locations 和 value 属性,TestContext 框架将尝试检测默认的 XML 资源位置。具体来说,GenericXmlContextLoader 和 GenericXmlWebContextLoader 根据测试类的名称检测默认位置。如果您的类名为 com.example.MyTest,则 GenericXmlContextLoader 从“classpath:com/example/MyTest-context.xml”加载您的应用程序上下文。

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

我应该担心这些消息吗?

显然,一点也不。

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

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

原文由 Ori Dar 发布,翻译遵循 CC BY-SA 3.0 许可协议

您可以将 AnnotationConfigContextLoader@Configuration. 结合使用

有关更多详细信息,请参见 此处

原文由 abhi 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题