Springboot單元測試無法讀取配置文件的解決方案
單元測試無法讀取配置文件
首先,測試類要加注解
表示該類是一個測試類,默認會加載resource文件夾下的配置文件。
如果想要指定配置文件:
@TestPropertySource("classpath:pay.properties")
深淵巨坑
spring boot 2,在進行單元測試的時候,不支持.yml文件!??!至文章日期,還未解決。
用yml文件的小伙伴別折騰了,測試文件夾下放一個properties文件吧!
單元測試加載配置文件啟動異常
報錯內(nèi)容:Failed to load ApplicationContext
Invalid bean definition with name 'dataSource' defined in URL [file:/D:/KaiFa/pals/IBC/target/classes/spring-mybatis.xml]: Could not resolve placeholder 'jdbc.driver' in string value "${jdbc.driver}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'jdbc.driver' in string value "${jdbc.driver}"
Could not resolve placeholder 'jdbc.driver' in string value "${jdbc.driver}"
貼一些關(guān)鍵錯誤,就不全貼了.錯誤說的在單元測試加載配置文件的時候無法加載jdbc.properties中的配置參數(shù)
最初以為是配置文件路徑寫錯了
經(jīng)過排查后沒有問題
檢查了引包 spring-test的版本是4.0.2.RELEASE
<dependency> ? ? <groupId>junit</groupId> ? ? <artifactId>junit</artifactId> ? ? <version>4.12</version> </dependency> <dependency> ? ? <groupId>org.springframework</groupId> ? ? <artifactId>spring-test</artifactId> ? ? <version>${spring.version}</version> </dependency>
這是單元測試
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath*:/spring-mvc.xml","classpath*:/spring-mybatis.xml"}) public class BaseDao { ? ? @Resource ? ? private HomeXmlService homeXmlService; ? ? @Test ? ? public void csh() { ? ? ? ? HomeDao c = new HomeDao(); ? ? ? ? homeXmlService.setXmlData(c); ? ? } }
最后確認是因為xml配置問題
在單元測試加載了spring-mvc和spring-mybatis兩個文件
在兩個配置中我分別加載了Properties配置文件。
spring-mybatis.xml
<bean id="propertyConfigurer" ? ?class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> ? ?<property name="location" value="classpath:jdbc.properties" /> </bean>
spring-mvc.xml
<bean class="com.ibc.util.CustomizedPlaceHolder"> ? ?<property name="locations"> ? ? ? <list> ? ? ? ? ?<value>classpath:prop/url.properties</value> ? ? ? ? ?<value>classpath:prop/synthetic-ratio.properties</value> ? ? ? </list> ? ?</property> </bean>
經(jīng)過不斷排查和發(fā)現(xiàn)是因為分別引用了properties的問題
后因為是spring-mybatis報錯,就在家在配置中加入
<property name="ignoreUnresolvablePlaceholders" value="true" />
調(diào)試后發(fā)現(xiàn)仍然有問題
最終確認,不止spring-mybaits,spring-mvc也要加這句,配置屬性的字面意思就是忽略不肯舍棄的占位符
問題解決。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring?Boot整合?NoSQL?數(shù)據(jù)庫?Redis詳解
這篇文章主要為大家介紹了Spring?Boot整合?NoSQL?數(shù)據(jù)庫?Redis詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09MyBatisPlus?大數(shù)據(jù)量查詢慢的問題解決
本文主要介紹了MyBatis?Plus?解決大數(shù)據(jù)量查詢慢問題,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02java配置多個過濾器優(yōu)先級以及幾個常用過濾器操作
這篇文章主要介紹了java配置多個過濾器優(yōu)先級以及幾個常用過濾器的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07Java利用MYSQL LOAD DATA LOCAL INFILE實現(xiàn)大批量導(dǎo)入數(shù)據(jù)到MySQL
Mysql load data的使用,MySQL的LOAD DATAINFILE語句用于高速地從一個文本文件中讀取行,并裝入一個表中2018-03-03java 獲取冒號后面的參數(shù)(正則)實現(xiàn)代碼
這篇文章主要介紹了java 獲取冒號后面的參數(shù)(正則)實現(xiàn)代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08Java編程實現(xiàn)統(tǒng)計數(shù)組中各元素出現(xiàn)次數(shù)的方法
這篇文章主要介紹了Java編程實現(xiàn)統(tǒng)計數(shù)組中各元素出現(xiàn)次數(shù)的方法,涉及java針對數(shù)組的遍歷、比較、運算等相關(guān)操作技巧,需要的朋友可以參考下2017-07-07