Java Spring 集成测试:无法检测默认资源位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36622871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:29:44  来源:igfitidea点击:

Spring Integration Testing: Could not detect default resource locations

javaspringspring-bootspring-test

提问by lanoxx

I am running integration tests for my Spring Boot application with Maven's Failsafe plugin. When I create a simple test such as this one:

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

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

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

And then run mvn verifyI see the following log entries just before the Spring application starts (e.g. even before the Spring Boot banner):

然后运行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]

Followed by the Spring Boot banner. The tests then run through without any error. While these messages are printed with an INFO log level and the tests run fine, I guess everything is fine, but I still find these messages irritating.So is there something wrong with my configuration? Should I worry about these messages?

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

Even if there was nothing wrong, I would still like to understand whats happending there and what the messages mean.

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

My maven-failsafe-pluginis just using the default configuration and my App.javais just a simple class annotated with @SpringBootApplication.

maven-failsafe-plugin只是使用默认配置,而我App.java只是一个用@SpringBootApplication.

UPDATE 1:

更新1:

Here is the configuration of my test plugins:

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

  <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>

I also have a spring-boot-starter-testdependency configured and I am using spring-boot-devtools. Besides that everything else is not test related.

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

The project it self is pretty standard, using hibernate and mysql and web-mvc for rest endpoints. I have two configuration classes for spring security on the class pass.

它本身的项目非常标准,使用 hibernate 和 mysql 以及 web-mvc 作为休息端点。我在班级通行证上有两个用于 spring 安全性的配置类。

In the main/resourcesfolder I have an application.propertiesfile and a log4j2.xml. There is no test/resourcesfolder, but when I just copy the main/resources folder to test/resourcesthe above log messages still appear.

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

UPDATE 2:I just created a small sample application trying to recreate the issue, and it seems the mentioned log-output starts to appear as soon as I put a log4j2.xmlfile into a resources folder. I tried putting it in src/mainor src/testor both with identical effects.

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

采纳答案by Ori Dar

This is the default behaviour when running Spring integration tests.

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

From the reference documentation

从参考文档

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".

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

So, it's not related whatsoever to Maven, log4j or the positioning/assistance of resource folder.

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

Should I worry about these messages?

我应该担心这些消息吗?

Not at all, apparently.

完全没有,显然。

but I still find these messages irritating

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

Don't know if and how to turn off that check (Course you can eliminate it by changing the log level of AbstractContextLoaderto WARN).

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

回答by abhi

You can use AnnotationConfigContextLoaderin combination with @Configuration.

您可以AnnotationConfigContextLoader结合使用@Configuration.

See herefor more details.

请参阅此处了解更多详情。

回答by AlexGera

You can annotate you test class with more specific configs for loader and use Java Config

您可以使用更具体的加载器配置来注释测试类并使用 Java Config

Something like:

就像是:

@ContextConfiguration(loader=AnnotationConfigContextLoader.class, classes = ScalaTestConfig.class)

Where ScalaTestConfig class is annotated with:

其中 ScalaTestConfig 类注释为:

@Configuration
@ComponentScan(basePackages = {"***", "***"})

Based on Kulasangar's link.

基于 Kulasangar 的链接。