Spring項(xiàng)目中使用Junit單元測(cè)試并配置數(shù)據(jù)源的操作
Spring 使用Junit單元測(cè)試并配置數(shù)據(jù)源
一、問題描述
由于公司項(xiàng)目中的數(shù)據(jù)源是配置在Tomcat中的server.xml中的,所以在使用Junit進(jìn)行單元測(cè)試的時(shí)候,無法獲取數(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è)試類中加載配置文件與獲取Bean
public class CommonDAOJdbc_StandardTest {
private volatile static BeanFactory factory;
@Test
public void testGetFirmCanOutBalance() {
// 獲取Bean
CommonDAO commonDAO = (CommonDAO) factory.getBean("commonDAO");
// 此處可調(diào)用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"); // 將測(cè)試用的數(shù)據(jù)源配置文件加入待加載列表
try {
factory = new ClassPathXmlApplicationContext(list.toArray(new String[list.size()]));
// 保證虛擬機(jī)退出之前 spring中singtleton對(duì)象自定義銷毀方法會(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ù)依賴 單元測(cè)試的一點(diǎn)想法
雖然我們會(huì)盡量保證測(cè)試的單純性,但是很多單元測(cè)試是測(cè)試數(shù)據(jù)依賴的,特別是數(shù)據(jù)庫(kù),如何保證測(cè)試的自動(dòng)性,可重復(fù)性、獨(dú)立性、專業(yè)性等特性,是一個(gè)比較棘手的問題。
一點(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è)試類的,在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)該能滿足多數(shù)情況,特殊情況的自行準(zhǔn)備測(cè)試數(shù)據(jù)。[/list]
這里面有這樣一些問題:
[*]單元測(cè)試自行準(zhǔn)備數(shù)據(jù),剛開始的時(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è)問題,剛開始的測(cè)試數(shù)據(jù)自行維護(hù),待穩(wěn)定后統(tǒng)一維護(hù),給人感覺好一點(diǎn),但不知道會(huì)有什么其他的問題。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA進(jìn)程已結(jié)束,退出代碼-1073741819 (0xC0000005)的bug
這篇文章主要介紹了IDEA進(jìn)程已結(jié)束,退出代碼-1073741819 (0xC0000005)的bug,本文通過實(shí)例代碼圖文的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
CompletableFuture并行處理List分批數(shù)據(jù)demo
這篇文章主要介紹了CompletableFuture并行處理List分批數(shù)據(jù)實(shí)現(xiàn)實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
SpringMVC中ModelAndView用法小結(jié)
本文主要介紹了SpringMVC中ModelAndView用法小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12
Eclipse新建項(xiàng)目不可選擇Java Project問題解決方案
這篇文章主要介紹了Eclipse新建項(xiàng)目不可選擇Java Project問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07

