欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springboot單元測(cè)試無(wú)法讀取配置文件的解決方案

 更新時(shí)間:2022年01月19日 10:27:47   作者:雙木枯榮  
這篇文章主要介紹了Springboot單元測(cè)試無(wú)法讀取配置文件的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

單元測(cè)試無(wú)法讀取配置文件

首先,測(cè)試類(lèi)要加注解

表示該類(lèi)是一個(gè)測(cè)試類(lèi),默認(rèn)會(huì)加載resource文件夾下的配置文件。

如果想要指定配置文件:

 
@TestPropertySource("classpath:pay.properties")

深淵巨坑

spring boot 2,在進(jìn)行單元測(cè)試的時(shí)候,不支持.yml文件?。?!至文章日期,還未解決。

用yml文件的小伙伴別折騰了,測(cè)試文件夾下放一個(gè)properties文件吧!

單元測(cè)試加載配置文件啟動(dòng)異常

報(bào)錯(cuò)內(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)鍵錯(cuò)誤,就不全貼了.錯(cuò)誤說(shuō)的在單元測(cè)試加載配置文件的時(shí)候無(wú)法加載jdbc.properties中的配置參數(shù)

最初以為是配置文件路徑寫(xiě)錯(cuò)了

經(jīng)過(guò)排查后沒(méi)有問(wèn)題

檢查了引包  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>

這是單元測(cè)試

@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);
? ? }
}

最后確認(rèn)是因?yàn)閤ml配置問(wèn)題

在單元測(cè)試加載了spring-mvc和spring-mybatis兩個(gè)文件

在兩個(gè)配置中我分別加載了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)過(guò)不斷排查和發(fā)現(xiàn)是因?yàn)榉謩e引用了properties的問(wèn)題

后因?yàn)槭莝pring-mybatis報(bào)錯(cuò),就在家在配置中加入

<property name="ignoreUnresolvablePlaceholders" value="true" />

調(diào)試后發(fā)現(xiàn)仍然有問(wèn)題

最終確認(rèn),不止spring-mybaits,spring-mvc也要加這句,配置屬性的字面意思就是忽略不肯舍棄的占位符

問(wèn)題解決。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論