Spring項(xiàng)目中使用Junit單元測(cè)試并配置數(shù)據(jù)源的操作
Spring 使用Junit單元測(cè)試并配置數(shù)據(jù)源
一、問(wèn)題描述
由于公司項(xiàng)目中的數(shù)據(jù)源是配置在Tomcat中的server.xml中的,所以在使用Junit進(jìn)行單元測(cè)試的時(shí)候,無(wú)法獲取數(shù)據(jù)源。
二、解決方案
由于項(xiàng)目集成了Spring的自動(dòng)注入等功能,所以在使用Junit進(jìn)行單元測(cè)試的時(shí)候需要保證Spring的配置文件都能被加載,同時(shí)需要保證連接數(shù)據(jù)庫(kù)的數(shù)據(jù)源必須被加載,這就需要配置單獨(dú)的數(shù)據(jù)源,具體方法如下:
- 新建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測(cè)試類(lèi)中加載配置文件與獲取Bean
public class CommonDAOJdbc_StandardTest { private volatile static BeanFactory factory; @Test public void testGetFirmCanOutBalance() { // 獲取Bean CommonDAO commonDAO = (CommonDAO) factory.getBean("commonDAO"); // 此處可調(diào)用CommonDAO類(lèi)中的方法 } @Before public void init() { System.out.println("加載spring配置開(kāi)始 ............"); ArrayList<String> list = new ArrayList<String>(); list.add("spring.xml"); // 將Sprint配置文件加入待加載列表 list.add("Spring_jndi_test.xml"); // 將測(cè)試用的數(shù)據(jù)源配置文件加入待加載列表 try { factory = new ClassPathXmlApplicationContext(list.toArray(new String[list.size()])); // 保證虛擬機(jī)退出之前 spring中singtleton對(duì)象自定義銷(xiāo)毀方法會(huì)執(zhí)行 ((AbstractApplicationContext) factory).registerShutdownHook(); } catch (Exception e) { e.printStackTrace(); System.out.println("加載配置文件時(shí)發(fā)生錯(cuò)誤" + e); } System.out.println("加載spring配置結(jié)束............."); } }
至此,便可以進(jìn)行Junit的單元測(cè)試,且數(shù)據(jù)源也能獲取了。
當(dāng)然,如果出現(xiàn)“java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver”,那么則需要Build Path -> Add Libraries … 引入ojdbc包即可。
Spring 數(shù)據(jù)庫(kù)依賴(lài) 單元測(cè)試的一點(diǎn)想法
雖然我們會(huì)盡量保證測(cè)試的單純性,但是很多單元測(cè)試是測(cè)試數(shù)據(jù)依賴(lài)的,特別是數(shù)據(jù)庫(kù),如何保證測(cè)試的自動(dòng)性,可重復(fù)性、獨(dú)立性、專(zhuān)業(yè)性等特性,是一個(gè)比較棘手的問(wèn)題。
一點(diǎn)想法:
[list][*]每個(gè)unit_test自行準(zhǔn)備數(shù)據(jù),在單元測(cè)試中進(jìn)行數(shù)據(jù)的維護(hù),設(shè)置rollback,保持測(cè)試的獨(dú)立性。
[*]測(cè)試數(shù)據(jù)統(tǒng)一準(zhǔn)備,單元測(cè)試前導(dǎo)入測(cè)試數(shù)據(jù)庫(kù),設(shè)置rollback
這里有兩種選擇。
- 1.可以應(yīng)用到整個(gè)單元測(cè)試類(lèi)的,在setup中添加,也可以在先有數(shù)據(jù)基礎(chǔ)上作修改。(因?yàn)槭莚ollback方式,不會(huì)對(duì)其他測(cè)試產(chǎn)生影響)
- 2.只針對(duì)具體testMethod的,在test中做 [*]兩種方式結(jié)合,統(tǒng)一數(shù)據(jù)準(zhǔn)備應(yīng)該能滿(mǎn)足多數(shù)情況,特殊情況的自行準(zhǔn)備測(cè)試數(shù)據(jù)。[/list]
這里面有這樣一些問(wèn)題:
[*]單元測(cè)試自行準(zhǔn)備數(shù)據(jù),剛開(kāi)始的時(shí)候比較方便,單時(shí)間長(zhǎng)了會(huì)有大量的重復(fù)數(shù)據(jù),數(shù)據(jù)雜亂。
[*]統(tǒng)一準(zhǔn)備數(shù)據(jù),測(cè)試數(shù)據(jù)需要統(tǒng)一維護(hù),以避免不同人修改,造成不必要的錯(cuò)誤,但這樣測(cè)試數(shù)據(jù)與測(cè)試邏輯分離,修改數(shù)據(jù)的人可能并不了解修改可能造成預(yù)期測(cè)試結(jié)果的改變,產(chǎn)生錯(cuò)誤不可避免。如果大家分人維護(hù),混亂不可避免,數(shù)據(jù)之間是有相關(guān)性的。
[*]兩種方式結(jié)合,如何結(jié)合也是一個(gè)問(wèn)題,剛開(kāi)始的測(cè)試數(shù)據(jù)自行維護(hù),待穩(wěn)定后統(tǒng)一維護(hù),給人感覺(jué)好一點(diǎn),但不知道會(huì)有什么其他的問(wèn)題。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot配置Profile實(shí)現(xiàn)多環(huán)境支持
這篇文章主要介紹了SpringBoot配置Profile實(shí)現(xiàn)多環(huán)境支持操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08java調(diào)用mysql存儲(chǔ)過(guò)程實(shí)例分析
這篇文章主要介紹了java調(diào)用mysql存儲(chǔ)過(guò)程的方法,以實(shí)例形式較為詳細(xì)的分析了mysql數(shù)據(jù)庫(kù)的建立和存儲(chǔ)過(guò)程的實(shí)現(xiàn)方法,需要的朋友可以參考下2015-06-06SpringBoot中ApplicationEvent的使用步驟詳解
ApplicationEvent類(lèi)似于MQ,是Spring提供的一種發(fā)布訂閱模式的事件處理方式,本文給大家介紹SpringBoot中ApplicationEvent的使用步驟詳解,感興趣的朋友跟隨小編一起看看吧2024-04-04SpringCloud?Gateway讀取Request?Body方式
這篇文章主要介紹了SpringCloud?Gateway讀取Request?Body方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03file.mkdir()、file.mkdirs()和file.createNewFile()的區(qū)別
本文主要介紹了file.mkdir()、file.mkdirs()和file.createNewFile()的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04Java使用DateFormatter格式化日期時(shí)間的方法示例
這篇文章主要介紹了Java使用DateFormatter格式化日期時(shí)間的方法,結(jié)合具體實(shí)例分析了java使用DateFormatter格式化日期時(shí)間的相關(guān)操作技巧,需要的朋友可以參考下2017-04-04SWT(JFace)體驗(yàn)之StyledText類(lèi)
有的時(shí)候Text需要實(shí)現(xiàn)這種那種的樣式。先提供在不使用StyledText類(lèi)的情況:2009-06-06SpringAOP中基于注解實(shí)現(xiàn)通用日志打印方法詳解
這篇文章主要介紹了SpringAOP中基于注解實(shí)現(xiàn)通用日志打印方法詳解,在日常開(kāi)發(fā)中,項(xiàng)目里日志是必不可少的,一般有業(yè)務(wù)日志,數(shù)據(jù)庫(kù)日志,異常日志等,主要用于幫助程序猿后期排查一些生產(chǎn)中的bug,需要的朋友可以參考下2023-12-12基于Elasticsearch5.4的常見(jiàn)問(wèn)題總結(jié)
下面小編就為大家分享一篇基于Elasticsearch5.4的常見(jiàn)問(wèn)題總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01