2016-04-14 40 views
2

我正在使用Maven的Failsafe插件爲我的Spring Boot應用程序運行集成測試。當我創建了一個簡單的測試,比如這一個:Spring集成測試:無法檢測默認資源位置

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

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

然後運行mvn verify我看到的只是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: [or[email protected]57c758ac, org.springframework.test[email protected]a9cd3b1, org.springframewor[email protected]13e39c73, org.springfra[email protected]64cd705f, org.springframew[email protected]9225652, org.sp[email protected]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作爲休息端點。我在班級通行證上有兩個彈簧安全性配置類。

main/resources文件夾中,我有一個application.properties文件和一個log4j2.xml。沒有test/resources文件夾,但是當我將主/資源文件夾複製到test/resources時,上述日誌消息仍然出現。

更新2: 我剛剛創建了一個小示例應用程序試圖重新的問題,似乎提到的日誌輸出開始,我把log4j2.xml文件到一個文件夾中的資源,一旦出現。我試圖把它放在src/mainsrc/test或兩者具有相同的效果。

+0

看起來很奇怪。如果這是使用默認配置,那麼您可以共享一個重現該問題的項目,對嗎? –

+0

請分享一些更多的細節,如項目結構,pom.xml測試配置文件。因爲當你運行集成測試時,在執行測試用例之前應該運行spring引導。 – Hareesh

+0

你有測試的資源文件夾嗎? – dambros

回答

4

這是運行Spring集成測試時的默認行爲。

從參考文檔

如果省略這兩個位置和價值@ContextConfiguration註解屬性,TestContext框架將嘗試檢測默認XML資源位置。具體來說,GenericXmlContextLoader和GenericXmlWebContextLoader基於測試類的名稱檢測默認位置。如果您的類名爲com.example.MyTest,則GenericXmlContextLoader會從「classpath:com/example/MyTest-context.xml」加載您的應用程序上下文。

所以,它不涉及到任何Maven的是,log4j或資源文件夾的位置/援助。

我應該擔心這些信息嗎?

顯然不是。

,但我仍然覺得這些消息刺激

不知道是否和如何關閉該檢查(當然你也可以通過改變AbstractContextLoader日誌級別設置爲WARN消除它)。

+0

感謝您的回答,它的工作原理與引用文檔中所述完全相同。很高興知道這個功能。我想現在我只是忽略這些日誌消息,但下一次我需要爲我的一個集成測試設置一個自定義配置,這將會非常方便。 – lanoxx

3

您可以結合使用AnnotationConfigContextLoader@Configuration.

詳情請參閱here

0

您可以標註您的測試類更具體的CONFIGS裝載機和使用Java配置

喜歡的東西:

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

凡ScalaTestConfig類都被註解:基於

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

Kulasangar的鏈接。