Spring項目中使用Junit單元測試并配置數(shù)據(jù)源的操作
Spring 使用Junit單元測試并配置數(shù)據(jù)源
一、問題描述
由于公司項目中的數(shù)據(jù)源是配置在Tomcat中的server.xml中的,所以在使用Junit進(jìn)行單元測試的時候,無法獲取數(shù)據(jù)源。
二、解決方案
由于項目集成了Spring的自動注入等功能,所以在使用Junit進(jìn)行單元測試的時候需要保證Spring的配置文件都能被加載,同時需要保證連接數(shù)據(jù)庫的數(shù)據(jù)源必須被加載,這就需要配置單獨的數(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測試類中加載配置文件與獲取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"); // 將測試用的數(shù)據(jù)源配置文件加入待加載列表
try {
factory = new ClassPathXmlApplicationContext(list.toArray(new String[list.size()]));
// 保證虛擬機(jī)退出之前 spring中singtleton對象自定義銷毀方法會執(zhí)行
((AbstractApplicationContext) factory).registerShutdownHook();
} catch (Exception e) {
e.printStackTrace();
System.out.println("加載配置文件時發(fā)生錯誤" + e);
}
System.out.println("加載spring配置結(jié)束.............");
}
}
至此,便可以進(jìn)行Junit的單元測試,且數(shù)據(jù)源也能獲取了。
當(dāng)然,如果出現(xiàn)“java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver”,那么則需要Build Path -> Add Libraries … 引入ojdbc包即可。
Spring 數(shù)據(jù)庫依賴 單元測試的一點想法
雖然我們會盡量保證測試的單純性,但是很多單元測試是測試數(shù)據(jù)依賴的,特別是數(shù)據(jù)庫,如何保證測試的自動性,可重復(fù)性、獨立性、專業(yè)性等特性,是一個比較棘手的問題。
一點想法:
[list][*]每個unit_test自行準(zhǔn)備數(shù)據(jù),在單元測試中進(jìn)行數(shù)據(jù)的維護(hù),設(shè)置rollback,保持測試的獨立性。
[*]測試數(shù)據(jù)統(tǒng)一準(zhǔn)備,單元測試前導(dǎo)入測試數(shù)據(jù)庫,設(shè)置rollback
這里有兩種選擇。
- 1.可以應(yīng)用到整個單元測試類的,在setup中添加,也可以在先有數(shù)據(jù)基礎(chǔ)上作修改。(因為是rollback方式,不會對其他測試產(chǎn)生影響)
- 2.只針對具體testMethod的,在test中做 [*]兩種方式結(jié)合,統(tǒng)一數(shù)據(jù)準(zhǔn)備應(yīng)該能滿足多數(shù)情況,特殊情況的自行準(zhǔn)備測試數(shù)據(jù)。[/list]
這里面有這樣一些問題:
[*]單元測試自行準(zhǔn)備數(shù)據(jù),剛開始的時候比較方便,單時間長了會有大量的重復(fù)數(shù)據(jù),數(shù)據(jù)雜亂。
[*]統(tǒng)一準(zhǔn)備數(shù)據(jù),測試數(shù)據(jù)需要統(tǒng)一維護(hù),以避免不同人修改,造成不必要的錯誤,但這樣測試數(shù)據(jù)與測試邏輯分離,修改數(shù)據(jù)的人可能并不了解修改可能造成預(yù)期測試結(jié)果的改變,產(chǎn)生錯誤不可避免。如果大家分人維護(hù),混亂不可避免,數(shù)據(jù)之間是有相關(guān)性的。
[*]兩種方式結(jié)合,如何結(jié)合也是一個問題,剛開始的測試數(shù)據(jù)自行維護(hù),待穩(wěn)定后統(tǒng)一維護(hù),給人感覺好一點,但不知道會有什么其他的問題。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA進(jìn)程已結(jié)束,退出代碼-1073741819 (0xC0000005)的bug
這篇文章主要介紹了IDEA進(jìn)程已結(jié)束,退出代碼-1073741819 (0xC0000005)的bug,本文通過實例代碼圖文的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
CompletableFuture并行處理List分批數(shù)據(jù)demo
這篇文章主要介紹了CompletableFuture并行處理List分批數(shù)據(jù)實現(xiàn)實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
SpringMVC中ModelAndView用法小結(jié)
本文主要介紹了SpringMVC中ModelAndView用法小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12
Eclipse新建項目不可選擇Java Project問題解決方案
這篇文章主要介紹了Eclipse新建項目不可選擇Java Project問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07

