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

Springboot單元測試無法讀取配置文件的解決方案

 更新時間:2022年01月19日 10:27:47   作者:雙木枯榮  
這篇文章主要介紹了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)文章

最新評論