解決@springboottest注解無法加載src/main/resources目錄下文件
Springboot微服務(wù)框架是目前越來越流行的框架,省去了很多繁瑣的xml配置。最近新啟了個(gè)項(xiàng)目,采用SpringBoot框架從頭搭建,中間也遇到過各種坑,現(xiàn)在先描述一下 Junit4單元測試之坑吧。
結(jié)論
@SpringBootTest注解,只會(huì)加載test路徑下的資源文件(即xml配置),并不會(huì)加載main路徑下的資源文件,這點(diǎn)很坑。。。
環(huán)境及問題描述
1.SpringBoot版本:2.0.0.RELEASE
2.通過項(xiàng)目入口類啟動(dòng)Application.main() OK,能正常處理web請(qǐng)求
3.啟動(dòng)單元測試,執(zhí)行測試類
提示Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.xx.xxx.service.SsoService' available
如下:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-02-02 16:26:10.537 ERROR 7812 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :***************************
APPLICATION FAILED TO START
***************************Description:
A component required a bean of type 'com.xx.xxx.service.SsoService' that could not be found.
Action:Consider defining a bean of type 'com.xx.xxx.service.SsoService' in your configuration.
2019-02-02 16:26:10.541 ERROR 7812 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@93b025] to prepare test instance [com.jd.id.activity.ControllerTest@b9178]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125) ~[spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:107) ~[spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190) ~[spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132) ~[spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:242) ~[spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227) [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246) [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12]
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) [junit-4.12.jar:4.12]
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) [junit-rt.jar:na]
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51) [junit-rt.jar:na]
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) [junit-rt.jar:na]
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) [junit-rt.jar:na]…………
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.xx.xxx.service.SsoService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1509)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:506)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:484)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:618)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:177)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:318)
... 41 more
Process finished with exit code -1
問題分析
1.首先com.xx.xxx.service.SsoService該類存在
并且項(xiàng)目正常啟動(dòng)時(shí),并沒有這個(gè)錯(cuò)誤提示,那肯定是單元測試配置的有問題了,首先檢查下相關(guān)的配置,@RunWith(SpringRunner.class)使用SpringRunner以便在測試開始的時(shí)候自動(dòng)創(chuàng)建spring的應(yīng)用上下文,沒毛病。
@SpringBootTest(classes = 啟動(dòng)類.class)配置也沒問題,網(wǎng)上有討論說springboot 1.4以下版本,要使用@SpringApplicationConfiguration(classes = 啟動(dòng)類.class),而我的項(xiàng)目版本是2.0,顯然這個(gè)配置也沒毛病。
代碼如下:
package com.xx.xxx.activity; import org.junit.After; import org.junit.Before; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.web.WebAppConfiguration; /** * 單元測試基類 */ @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) //由于是Web項(xiàng)目,Junit需要模擬ServletContext,因此我們需要給我們的測試類加上@WebAppConfiguration。 @WebAppConfiguration public class BaseTest { @Before public void init() { System.out.println("開始測試----------------"); } @After public void destory() { System.out.println("結(jié)束測試----------------"); } }
2.再看下pom文件的配置
引用了對(duì)應(yīng)的test包,也沒毛病
? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? <artifactId>spring-boot-starter-test</artifactId> ? ? ? ? ? ? <version>${springboot.version}</version> ? ? ? ? ? ? <scope>test</scope> ? ? ? ? </dependency>
3.這個(gè)類是在src/main/resources目錄下的資源文件里配置
即xml中配置的bean,而src/test下并沒有resources目錄,莫非是這個(gè)原因?于是乎在test目錄下創(chuàng)建resources路徑,并把xml文件拷貝至test對(duì)應(yīng)的路徑下,結(jié)果能夠正常進(jìn)行單元測試了,數(shù)據(jù)也寫入到數(shù)據(jù)庫中,終于松了口氣 -
準(zhǔn)備就此作罷,不想在這個(gè)問題上浪費(fèi)時(shí)間了,但是再做考慮,這樣做欠妥,畢竟我每次改完xml文件的配置,必須得拷貝到test目錄對(duì)應(yīng)的文件夾下,這拷來拷去,并不是最好的解決方案啊,能不能打包時(shí),把src/main/resources的資源文件也拷貝到src/test/resources中?這樣就不用手工拷貝了。
答案是:當(dāng)然有了,看pom.xml配置:
<build> <finalName>id-web-activity-root</finalName> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*.*</include> </includes> </resource> </resources> <!--單元測試時(shí)引用src/main/resources下的資源文件--> <testResources> <testResource> <directory>src/test/resources</directory> </testResource> <testResource> <directory>src/main/resources</directory> </testResource> </testResources> </build>
這時(shí)再去執(zhí)行單元測試,正常執(zhí)行,同時(shí)test-classes路徑下已經(jīng)有了xml資源配置文件,如下圖:
單元測試的坑算是填了,再接著填其他坑吧。。。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Springboot 項(xiàng)目讀取Resources目錄下的文件(推薦)
- 解決springboot項(xiàng)目找不到resources目錄下的資源問題
- springboot項(xiàng)目讀取resources目錄下的文件的9種方式
- springboot實(shí)現(xiàn)jar運(yùn)行復(fù)制resources文件到指定的目錄(思路詳解)
- SpringBoot中讀取jar包中的resources目錄下的文件的三種方式
- SpringBoot如何讀取resources目錄下的文件
- SpringBoot實(shí)現(xiàn)本地上傳文件到resources目錄
- Springboot獲取jar包中resources資源目錄下的文件
- Springboot項(xiàng)目啟動(dòng)不加載resources目錄下的文件問題
- SpringBoot下獲取resources目錄下文件的常用方法
相關(guān)文章
Java ArrayList集合詳解(Java動(dòng)態(tài)數(shù)組)
這篇文章主要介紹了Java ArrayList集合詳解(Java動(dòng)態(tài)數(shù)組),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10關(guān)于JAVA經(jīng)典算法40題(超實(shí)用版)
本篇文章小編為大家介紹一下,關(guān)于JAVA經(jīng)典算法40題(超實(shí)用版),有需要的朋友可以參考一下2013-04-04Java實(shí)現(xiàn)ftp文件上傳下載解決慢中文亂碼多個(gè)文件下載等問題
這篇文章主要介紹了Java實(shí)現(xiàn)ftp文件上傳下載解決慢中文亂碼多個(gè)文件下載等問題的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10如何使用SpringBoot集成Kafka實(shí)現(xiàn)用戶數(shù)據(jù)變更后發(fā)送消息
Spring Boot集成Kafka實(shí)現(xiàn)用戶數(shù)據(jù)變更后,向其他廠商發(fā)送消息,我們需要考慮配置Kafka連接、創(chuàng)建Kafka Producer發(fā)送消息、監(jiān)聽用戶數(shù)據(jù)變更事件,并將事件轉(zhuǎn)發(fā)到Kafka,本文分步驟給大家講解使用SpringBoot集成Kafka實(shí)現(xiàn)用戶數(shù)據(jù)變更后發(fā)送消息,感興趣的朋友一起看看吧2024-07-07如何通過eclipse web項(xiàng)目導(dǎo)入itellij idea并啟動(dòng)
這篇文章主要介紹了如何通過eclipse web項(xiàng)目導(dǎo)入itellij idea并啟動(dòng),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12Arthas在線java進(jìn)程診斷工具在線調(diào)試神器詳解
Arthas是 Alibaba 開源的Java診斷工具,深受開發(fā)者喜愛。這篇文章主要介紹了Arthas在線java進(jìn)程診斷工具 在線調(diào)試神器,需要的朋友可以參考下2021-11-11解決SpringBoot application.yaml文件配置schema 無法執(zhí)行sql問題
這篇文章主要介紹了解決SpringBoot application.yaml文件配置schema 無法執(zhí)行sql問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08java實(shí)現(xiàn)大文件導(dǎo)出的實(shí)現(xiàn)與優(yōu)化
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)大文件導(dǎo)出的實(shí)現(xiàn)與優(yōu)化的相關(guān)資料,文中的示例代碼講解詳細(xì),對(duì)我們深入了解java有一定的幫助,感興趣的小伙伴可以了解下2023-11-11