Spring項目中使用Junit單元測試并配置數據源的操作
Spring 使用Junit單元測試并配置數據源
一、問題描述
由于公司項目中的數據源是配置在Tomcat中的server.xml中的,所以在使用Junit進行單元測試的時候,無法獲取數據源。
二、解決方案
由于項目集成了Spring的自動注入等功能,所以在使用Junit進行單元測試的時候需要保證Spring的配置文件都能被加載,同時需要保證連接數據庫的數據源必須被加載,這就需要配置單獨的數據源,具體方法如下:
- 新建spring_jndi_test.xml
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <beans:property name="driverClassName" value="oracle.jdbc.OracleDriver" /> <beans:property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:sjk" /> <beans:property name="username" value="username" /> <beans:property name="password" value="password" /> </beans:bean> </beans:beans>
- 在Junit測試類中加載配置文件與獲取Bean
public class CommonDAOJdbc_StandardTest { private volatile static BeanFactory factory; @Test public void testGetFirmCanOutBalance() { // 獲取Bean CommonDAO commonDAO = (CommonDAO) factory.getBean("commonDAO"); // 此處可調用CommonDAO類中的方法 } @Before public void init() { System.out.println("加載spring配置開始 ............"); ArrayList<String> list = new ArrayList<String>(); list.add("spring.xml"); // 將Sprint配置文件加入待加載列表 list.add("Spring_jndi_test.xml"); // 將測試用的數據源配置文件加入待加載列表 try { factory = new ClassPathXmlApplicationContext(list.toArray(new String[list.size()])); // 保證虛擬機退出之前 spring中singtleton對象自定義銷毀方法會執(zhí)行 ((AbstractApplicationContext) factory).registerShutdownHook(); } catch (Exception e) { e.printStackTrace(); System.out.println("加載配置文件時發(fā)生錯誤" + e); } System.out.println("加載spring配置結束............."); } }
至此,便可以進行Junit的單元測試,且數據源也能獲取了。
當然,如果出現“java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver”,那么則需要Build Path -> Add Libraries … 引入ojdbc包即可。
Spring 數據庫依賴 單元測試的一點想法
雖然我們會盡量保證測試的單純性,但是很多單元測試是測試數據依賴的,特別是數據庫,如何保證測試的自動性,可重復性、獨立性、專業(yè)性等特性,是一個比較棘手的問題。
一點想法:
[list][*]每個unit_test自行準備數據,在單元測試中進行數據的維護,設置rollback,保持測試的獨立性。
[*]測試數據統(tǒng)一準備,單元測試前導入測試數據庫,設置rollback
這里有兩種選擇。
- 1.可以應用到整個單元測試類的,在setup中添加,也可以在先有數據基礎上作修改。(因為是rollback方式,不會對其他測試產生影響)
- 2.只針對具體testMethod的,在test中做 [*]兩種方式結合,統(tǒng)一數據準備應該能滿足多數情況,特殊情況的自行準備測試數據。[/list]
這里面有這樣一些問題:
[*]單元測試自行準備數據,剛開始的時候比較方便,單時間長了會有大量的重復數據,數據雜亂。
[*]統(tǒng)一準備數據,測試數據需要統(tǒng)一維護,以避免不同人修改,造成不必要的錯誤,但這樣測試數據與測試邏輯分離,修改數據的人可能并不了解修改可能造成預期測試結果的改變,產生錯誤不可避免。如果大家分人維護,混亂不可避免,數據之間是有相關性的。
[*]兩種方式結合,如何結合也是一個問題,剛開始的測試數據自行維護,待穩(wěn)定后統(tǒng)一維護,給人感覺好一點,但不知道會有什么其他的問題。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot配置Profile實現多環(huán)境支持
這篇文章主要介紹了SpringBoot配置Profile實現多環(huán)境支持操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08SpringBoot中ApplicationEvent的使用步驟詳解
ApplicationEvent類似于MQ,是Spring提供的一種發(fā)布訂閱模式的事件處理方式,本文給大家介紹SpringBoot中ApplicationEvent的使用步驟詳解,感興趣的朋友跟隨小編一起看看吧2024-04-04SpringCloud?Gateway讀取Request?Body方式
這篇文章主要介紹了SpringCloud?Gateway讀取Request?Body方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03file.mkdir()、file.mkdirs()和file.createNewFile()的區(qū)別
本文主要介紹了file.mkdir()、file.mkdirs()和file.createNewFile()的區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-04-04Java使用DateFormatter格式化日期時間的方法示例
這篇文章主要介紹了Java使用DateFormatter格式化日期時間的方法,結合具體實例分析了java使用DateFormatter格式化日期時間的相關操作技巧,需要的朋友可以參考下2017-04-04