Maven項(xiàng)目中Junit對(duì)Spring進(jìn)行單元測(cè)試方式
主要步驟:
1.在工程的pom文件中增加spring-test的依賴
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>2.使用springframework提供的單元測(cè)試
新建測(cè)試類,并在該類上加上兩個(gè)注解:
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations={“classpath*:ApplicationContext.xml”})
- @RunWith 大家并不陌生,junit4里用它來(lái)做junit加載器
- @ContextConfiguration 主要用來(lái)加載spring的配置文件路徑:是一個(gè)字符串?dāng)?shù)組,可以加載多個(gè)spring配置文件
測(cè)試代碼如下:
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:ApplicationContext.xml"})
public class EmpolyeeTest {
@Autowired
ApplicationContext ctx;
@Test
public void testEmployee(){
Employee employee =(Employee) ctx.getBean("employee");
assertEquals("zhangsan",employee.getName());
}
}
3.封裝基于AbstractJUnit4SpringContextTests的測(cè)試基類
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:ApplicationContext.xml"})
public class SpringTest extends AbstractJUnit4SpringContextTests {
public <T> T getBean(Class<T> type){
return applicationContext.getBean(type);
}
public Object getBean(String beanName){
return applicationContext.getBean(beanName);
}
protected ApplicationContext getContext(){
return applicationContext;
}
然后其他測(cè)試類只需要繼承該類即可,可以省去每次都要綁定Application對(duì)象。
4.當(dāng)項(xiàng)目變得復(fù)雜
其中的spring配置文件被拆分成了多個(gè),這樣該如何引入多個(gè)配置文件呢?如下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:spring-ctx-service.xml", "classpath*:spring-ctx-dao.xml" })
這樣就可以輕松的引入多個(gè)spring的配置文件了。
或者配置符合某一個(gè)正則表達(dá)式的一類文件,如:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath*:spring-ctx-*.xml")
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java轉(zhuǎn)換時(shí)區(qū)時(shí)間過(guò)程詳解
這篇文章主要介紹了java轉(zhuǎn)換時(shí)區(qū)時(shí)間過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
修改Maven settings.xml 后配置未生效的解決
這篇文章主要介紹了修改Maven settings.xml 后配置未生效的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
java和c/c++ 數(shù)據(jù)類型長(zhǎng)度的比較
本篇文章主要是對(duì)java和c/c++ 數(shù)據(jù)類型長(zhǎng)度的進(jìn)行了詳細(xì)的比較。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01
SpringMVC中參數(shù)綁定問題實(shí)例詳解
springmvc是用來(lái)處理頁(yè)面的一些請(qǐng)求,然后將數(shù)據(jù)再通過(guò)視圖返回給用戶的,下面這篇文章主要給大家介紹了關(guān)于SpringMVC中參數(shù)綁定問題的相關(guān)資料,需要的朋友可以參考下2022-04-04
java 與testng利用XML做數(shù)據(jù)源的數(shù)據(jù)驅(qū)動(dòng)示例詳解
這篇文章主要介紹了java 與testng利用XML做數(shù)據(jù)源的數(shù)據(jù)驅(qū)動(dòng)示例詳解的相關(guān)資料,需要的朋友可以參考下2017-01-01

