Spring Junit單元測(cè)試加載配置文件失敗問(wèn)題
JUnit是Java中最有名的單元測(cè)試框架,用于編寫(xiě)和運(yùn)行可重復(fù)的測(cè)試,多數(shù)Java的開(kāi)發(fā)環(huán)境都已經(jīng)集成了JUnit作為單元測(cè)試的工具。好的單元測(cè)試能極大的提高開(kāi)發(fā)效率和代碼質(zhì)量。
使用SpringJunit單元測(cè)試,通過(guò)@ContextConfiguration加載配置文件后,只會(huì)在src/test/resources目錄下尋找配置文件,不會(huì)加載src/main/resources中的。
這樣就導(dǎo)致了項(xiàng)目可以正常啟動(dòng),但是單元測(cè)試時(shí)會(huì)提示找不到注入的類(lèi)。
可以通過(guò)pom.xml配置來(lái)解決該問(wèn)題:
<build> <finalName>demo1</finalName> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*.*</include> </includes> </resource> </resources> <!--單元測(cè)試時(shí)引用src/main/resources下的資源文件--> <testResources> <testResource> <directory>src/test/resources</directory> </testResource> <testResource> <directory>src/main/resources</directory> </testResource> </testResources></build>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python公司內(nèi)項(xiàng)目對(duì)接釘釘審批流程的實(shí)現(xiàn)2. Python操作Excel工作簿的示例代碼(*.xlsx)3. Python 簡(jiǎn)介4. Python 利用flask搭建一個(gè)共享服務(wù)器的步驟5. Python importlib模塊重載使用方法詳解6. python用zip壓縮與解壓縮7. Python中Anaconda3 安裝gdal庫(kù)的方法8. Notepad++如何配置python?配置python操作流程詳解9. Python自動(dòng)化之定位方法大殺器xpath10. Python本地及虛擬解釋器配置過(guò)程解析
