国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
使用 Spring 進(jìn)行單元測試

概述

單元測試和集成測試在我們的軟件開發(fā)整個流程中占有舉足輕重的地位,一方面,程序員通過編寫單元測試來驗(yàn)證自己程序的有效性,另外一方面,管理者通過持續(xù)自動的執(zhí)行單元測試和分析單元測試的覆蓋率等來確保軟件本身的質(zhì)量。這里,我們先不談單元測試本身的重要性,對于目前大多數(shù)的基于 Java 的企業(yè)應(yīng)用軟件來說,Spring 已經(jīng)成為了標(biāo)準(zhǔn)配置,一方面它實(shí)現(xiàn)了程序之間的低耦合度,另外也通過一些配置減少了企業(yè)軟件集成的工作量,例如和 Hibernate、Struts 等的集成。那么,有個問題,在普遍使用 Spring 的應(yīng)用程序中,我們?nèi)绾稳プ鰡卧獪y試?或者說,我們怎么樣能高效的在 Spring 生態(tài)系統(tǒng)中實(shí)現(xiàn)各種單元測試手段?這就是本文章要告訴大家的事情。

單元測試目前主要的框架包括 Junit、TestNG,還有些 MOCK 框架,例如 Jmock、Easymock、PowerMock 等,這些都是單元測試的利器,但是當(dāng)把他們用在 Spring 的開發(fā)環(huán)境中,還是那么高效么?還好,Spring 提供了單元測試的強(qiáng)大支持,主要特性包括:

  • 支持主流的測試框架 Junit 和 TestNG
  • 支持在測試類中使用依賴注入 Denpendency Injection
  • 支持測試類的自動化事務(wù)管理
  • 支持使用各種注釋標(biāo)簽,提高開發(fā)效率和代碼簡潔性
  • Spring 3.1 更是支持在測試類中使用非 XML 配置方法和基于 Profile 的 bean 配置模式

通過閱讀本文,您能夠快速的掌握基于 Spring TestContext 框架的測試方法,并了解基本的實(shí)現(xiàn)原理。本文將提供大量測試標(biāo)簽的使用方法,通過這些標(biāo)簽,開發(fā)人員能夠極大的減少編碼工作量。OK,現(xiàn)在讓我們開始 Spring 的測試之旅吧!

原來我們是怎么做的

這里先展示一個基于 Junit 的單元測試,這個單元測試運(yùn)行在基于 Spring 的應(yīng)用程序中,需要使用 Spring 的相關(guān)配置文件來進(jìn)行測試。相關(guān)類圖如下:

數(shù)據(jù)庫表

假設(shè)有一個員工賬號表,保存了員工的基本賬號信息,表結(jié)構(gòu)如下:

  • ID:整數(shù)類型,唯一標(biāo)識
  • NAME:字符串,登錄賬號
  • SEX:字符串,性別
  • AGE:字符串,年齡

假設(shè)表已經(jīng)建好,且內(nèi)容為空。

測試工程目錄結(jié)構(gòu)和依賴 jar 包

在 Eclipse 中,我們可以展開工程目錄結(jié)構(gòu),看到如下圖所示的工程目錄結(jié)構(gòu)和依賴的 jar 包列表:

您需要引入的 jar 包括:

  • cglib-nodep-2.2.3.jar
  • commons-logging.jar
  • hsqldb.jar
  • Junit-4.5.jar
  • log4j-1.2.14.jar
  • Spring-asm-3.2.0.M1.jar
  • Spring-beans-3.2.0.M1.jar
  • Spring-context-3.2.0.M1.jar
  • Spring-core-3.2.0.M1.jar
  • Spring-expression-3.2.0.M1.jar
  • Spring-jdbc-3.2.0.M1.jar
  • Spring-test-3.2.0.M1.jar
  • Spring-tx-3.2.0.M1.jar
  • testng-6.8.jar

其中的 hsqldb 是我們測試用數(shù)據(jù)庫。


圖 1. 工程目錄結(jié)構(gòu)
 

類總體介紹

假設(shè)我們現(xiàn)在有一個基于 Spring 的應(yīng)用程序,除了 MVC 層,還包括業(yè)務(wù)層和數(shù)據(jù)訪問層,業(yè)務(wù)層有一個類 AccountService,負(fù)責(zé)處理賬號類的業(yè)務(wù),其依賴于數(shù)據(jù)訪問層 AccountDao 類,此類提供了基于 Spring Jdbc Template 實(shí)現(xiàn)的數(shù)據(jù)庫訪問方法,AccountService 和 AccountDao 以及他們之間的依賴關(guān)系都是通過 Spring 配置文件進(jìn)行管理的。

現(xiàn)在我們要對 AccountService 類進(jìn)行測試,在不使用 Spring 測試方法之前,我們需要這樣做:


清單 1. Account.Java

此類代表賬號的基本信息,提供 getter 和 setter 方法。

 package domain;  public class Account { 	 public static final String SEX_MALE = "male"; 	 public static final String SEX_FEMALE = "female"; 		 private int id; 	 private String name; 	 private int age; 	 private String sex;      public String toString() { 	    return String.format("Account[id=%d,name=%s,age:%d,sex:%s]",id,name,age,sex); 	 } 	 public int getId() { 		 return id; 	 } 	 public void setId(int id) { 		 this.id = id; 	 } 	 public String getName() { 		 return name; 	 } 	 public void setName(String name) { 		 this.name = name; 	 } 	 public int getAge() { 		 return age; 	 } 	 public void setAge(int age) { 		 this.age = age; 	 } 	 public String getSex() { 		 return sex; 	 } 	 public void setSex(String sex) { 		 this.sex = sex; 	 } 	     public static Account getAccount(int id,String name,int age,String sex) { 		 Account acct = new Account(); 		 acct.setId(id); 		 acct.setName(name); 		 acct.setAge(age); 		 acct.setSex(sex); 		 return acct; 	 }  } 

注意上面的 Account 類有一個 toString() 方法和一個靜態(tài)的 getAccount 方法,getAccount 方法用于快速獲取 Account 測試對象。


清單 2. AccountDao.Java

這個 DAO 我們這里為了簡單起見,采用 Spring Jdbc Template 來實(shí)現(xiàn)。

 package DAO;  import Java.sql.ResultSet;  import Java.sql.SQLException;  import Java.util.HashMap;  import Java.util.List;  import Java.util.Map;  import org.Springframework.context.ApplicationContext;  import org.Springframework.context.support.ClassPathXmlApplicationContext;  import org.Springframework.jdbc.core.RowMapper;  import org.Springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;  import org.Springframework.jdbc.core.simple.ParameterizedRowMapper;  import domain.Account;  public class AccountDao extends NamedParameterJdbcDaoSupport { 	 public void saveAccount(Account account) { 		 String sql = "insert into tbl_account(id,name,age,sex) " + 				"values(:id,:name,:age,:sex)"; 		 Map paramMap = new HashMap(); 		 paramMap.put("id", account.getId()); 		 paramMap.put("name", account.getName()); 		 paramMap.put("age", account.getAge()); 		 paramMap.put("sex",account.getSex()); 		 getNamedParameterJdbcTemplate().update(sql, paramMap); 	 } 		 public Account getAccountById(int id) { 		 String sql = "select id,name,age,sex from tbl_account where id=:id"; 		 Map paramMap = new HashMap(); 		 paramMap.put("id", id); 		 List<Account> matches = getNamedParameterJdbcTemplate().query(sql, 		 paramMap,new ParameterizedRowMapper<Account>() { 					 @Override 					 public Account mapRow(ResultSet rs, int rowNum) 							 throws SQLException { 						 Account a = new Account(); 						 a.setId(rs.getInt(1)); 						 a.setName(rs.getString(2)); 						 a.setAge(rs.getInt(3)); 						 a.setSex(rs.getString(4)); 						 return a; 					 } 					 }); 		 return matches.size()>0?matches.get(0):null; 	 } 	 } 

AccountDao 定義了幾個賬號對象的數(shù)據(jù)庫訪問方法:

  • saveAccount:負(fù)責(zé)把傳入的賬號對象入庫
  • getAccountById:負(fù)責(zé)根據(jù) Id 查詢賬號

清單 3. AccountService.Java
 package service;  import org.apache.commons.logging.Log;  import org.apache.commons.logging.LogFactory;  import org.Springframework.beans.factory.annotation.Autowired;  import DAO.AccountDao;  import domain.Account;  public class AccountService { 	 private static final Log log = LogFactory.getLog(AccountService.class); 		 @Autowired 	 private AccountDao accountDao; 		 public Account getAccountById(int id) { 		 return accountDao.getAccountById(id); 	 } 		 public void insertIfNotExist(Account account) { 		 Account acct = accountDao.getAccountById(account.getId()); 		 if(acct==null) { 			 log.debug("No "+account+" found,would insert it.");              accountDao.saveAccount(account); 		 } 		 acct = null; 	 } 		 } 

AccountService 包括下列方法:

  • getAccountById:根據(jù) Id 查詢賬號信息
  • insertIfNotExist:根據(jù)傳入的對象插入數(shù)據(jù)庫

其依賴的 DAO 對象 accountDao 是通過 Spring 注釋標(biāo)簽 @Autowired 自動注入的。


清單 4. Spring 配置文件

上述幾個類的依賴關(guān)系是通過 Spring 進(jìn)行管理的,配置文件如下:

 <beans xmlns="http://www.Springframework.org/schema/beans"	 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.Springframework.org/schema/context" 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 "> 	 <context:annotation-config/>  <bean id="datasource" class=" org.Springframework.jdbc.datasource.DriverManagerDataSource"> 		 <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> 		 <property name="url" value="jdbc:hsqldb:hsql://localhost" /> 		 <property name="username" value="sa" /> 		 <property name="password" value="" /> 	 </bean> 	 <bean id="initer" init-method="init" class="service.Initializer"> 	 </bean>  <bean id="accountDao" depends-on="initer" class="DAO.AccountDao"> 		 <property name="dataSource" ref="datasource" /> 	 </bean>  <bean id="accountService" class="service.AccountService"> 	 </bean>  </beans> 

注意其中的“<context:annotation-config/>”的作用,這個配置啟用了 Spring 對 Annotation 的支持,這樣在我們的測試類中 @Autowired 注釋才會起作用(如果用了 Spring 測試框架,則不需要這樣的配置項(xiàng),稍后會演示)。另外還有一個 accountDao 依賴的 initer bean, 這個 bean 的作用是加載 log4j 日志環(huán)境,不是必須的。

另外還有一個要注意的地方,就是 datasource 的定義,由于我們使用的是 Spring Jdbc Template,所以只要定義一個 org.Springframework.jdbc.datasource.DriverManagerDataSource 類型的 datasource 即可。這里我們使用了簡單的數(shù)據(jù)庫 HSQL、Single Server 運(yùn)行模式,通過 JDBC 進(jìn)行訪問。實(shí)際測試中,大家可以選擇 Oracle 或者 DB2、Mysql 等。

好,萬事具備,下面我們來用 Junit4 框架測試 accountService 類。代碼如下:


清單 5. AccountServiceOldTest.Java
 package service;  import static org.Junit.Assert.assertEquals;  import org.Junit.BeforeClass;  import org.Junit.Test;  import org.Springframework.context.ApplicationContext;  import org.Springframework.context.support.ClassPathXmlApplicationContext;  import domain.Account;  public class AccountServiceOldTest { 	 private static AccountService service; 		 @BeforeClass 	 public static void init() { 		 ApplicationContext  context = new ClassPathXmlApplicationContext("config/Spring-db-old.xml"); 		 service = (AccountService)context.getBean("accountService"); 	 } 			 @Test 	 public void testGetAcccountById() {  Account acct = Account.getAccount(1, "user01", 18, "M"); 		 Account acct2 = null; 		 try {  service.insertIfNotExist(acct); 			 acct2 = service.getAccountById(1); 			 assertEquals(acct, acct2); 		 } catch (Exception ex) { 			 fail(ex.getMessage()); 		 } finally { 			 service.removeAccount(acct); 		 }  }  } 

注意上面的 Junit4 注釋標(biāo)簽,第一個注釋標(biāo)簽 @BeforeClass,用來執(zhí)行整個測試類需要一次性初始化的環(huán)境,這里我們用 Spring 的 ClassPathXmlApplicationContext 從 XML 文件中加載了上面定義的 Spring 配置文件,并從中獲得了 accountService 的實(shí)例。第二個注釋標(biāo)簽 @Test 用來進(jìn)行實(shí)際的測試。

測試過程:我們先獲取一個 Account 實(shí)例對象,然后通過 service bean 插入數(shù)據(jù)庫中,然后通過 getAccountById 方法從數(shù)據(jù)庫再查詢這個記錄,如果能獲取,則判斷兩者的相等性;如果相同,則表示測試成功。成功后,我們嘗試刪除這個記錄,以利于下一個測試的進(jìn)行,這里我們用了 try-catch-finally 來保證賬號信息會被清除。

執(zhí)行測試:(在 Eclipse 中,右鍵選擇 AccountServiceOldTest 類,點(diǎn)擊 Run as Junit test 選項(xiàng)),得到的結(jié)果如下:

執(zhí)行測試的結(jié)果

在 Eclipse 的 Junit 視圖中,我們可以看到如下的結(jié)果:


圖 2. 測試的結(jié)果
 

對于這種不使用 Spring test 框架進(jìn)行的單元測試,我們注意到,需要做這些工作:

  • 在測試開始之前,需要手工加載 Spring 的配置文件,并獲取需要的 bean 實(shí)例
  • 在測試結(jié)束的時候,需要手工清空搭建的數(shù)據(jù)庫環(huán)境,比如清除您插入或者更新的數(shù)據(jù),以保證對下一個測試沒有影響

另外,在這個測試類中,我們還不能使用 Spring 的依賴注入特性。一切都靠手工編碼實(shí)現(xiàn)。好,那么我們看看 Spring test 框架能做到什么。

首先我們修改一下 Spring 的 XML 配置文件,刪除 <context:annotation-config/> 行,其他不變。


清單 6. Spring-db1.xml
 <beans xmlns="http://www.Springframework.org/schema/beans"	 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.Springframework.org/schema/beans  http://www.Springframework.org/schema/beans/Spring-beans-3.2.xsd">  <bean id="datasource"  class="org.Springframework.jdbc.datasource.DriverManagerDataSource"> 		 <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> 		 <property name="url" value="jdbc:hsqldb:hsql://localhost" /> 		 <property name="username" value="sa"/> 		 <property name="password" value=""/> 	 </bean>  <bean id="transactionManager"  class="org.Springframework.jdbc.datasource.DataSourceTransactionManager">  		 <property name="dataSource" ref="datasource"></property>  	 </bean>  	 <bean id="initer" init-method="init" class="service.Initializer">  	 </bean>  <bean id="accountDao" depends-on="initer" class="DAO.AccountDao">  		 <property name="dataSource" ref="datasource"/>  	 </bean>  	 <bean id="accountService" class="service.AccountService">  	 </bean>  </beans> 

其中的 transactionManager 是 Spring test 框架用來做事務(wù)管理的管理器。


清單 7. AccountServiceTest1.Java
 package service;  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.test.context.ContextConfiguration;  import org.Springframework.test.context.Junit4.SpringJUnit4ClassRunner;  import org.Springframework.transaction.annotation.Transactional;  import domain.Account;  @RunWith(SpringJUnit4ClassRunner.class)  @ContextConfiguration("/config/Spring-db1.xml")  @Transactional  public class AccountServiceTest1 { 	 @Autowired 	 private AccountService service; 		 @Test 	 public void testGetAcccountById() {  Account acct = Account.getAccount(1, "user01", 18, "M"); 		 service.insertIfNotExist(acct); 		 Account acct2 = service.getAccountById(1); 		 assertEquals(acct,acct2); 	 }  } 

對這個類解釋一下:

  • @RunWith 注釋標(biāo)簽是 Junit 提供的,用來說明此測試類的運(yùn)行者,這里用了 SpringJUnit4ClassRunner,這個類是一個針對 Junit 運(yùn)行環(huán)境的自定義擴(kuò)展,用來標(biāo)準(zhǔn)化在 Spring 環(huán)境中 Junit4.5 的測試用例,例如支持的注釋標(biāo)簽的標(biāo)準(zhǔn)化
  • @ContextConfiguration 注釋標(biāo)簽是 Spring test context 提供的,用來指定 Spring 配置信息的來源,支持指定 XML 文件位置或者 Spring 配置類名,這里我們指定 classpath 下的 /config/Spring-db1.xml 為配置文件的位置
  • @Transactional 注釋標(biāo)簽是表明此測試類的事務(wù)啟用,這樣所有的測試方案都會自動的 rollback,即您不用自己清除自己所做的任何對數(shù)據(jù)庫的變更了
  • @Autowired 體現(xiàn)了我們的測試類也是在 Spring 的容器中管理的,他可以獲取容器的 bean 的注入,您不用自己手工獲取要測試的 bean 實(shí)例了
  • testGetAccountById 是我們的測試用例:注意和上面的 AccountServiceOldTest 中相同的測試方法的對比,這里我們不用再 try-catch-finally 了,事務(wù)管理自動運(yùn)行,當(dāng)我們執(zhí)行完成后,所有相關(guān)變更會被自動清除

執(zhí)行結(jié)果

在 Eclipse 的 Junit 視圖中,我們可以看到如下的結(jié)果:


圖 3. 執(zhí)行結(jié)果
 

小結(jié)

如果您希望在 Spring 環(huán)境中進(jìn)行單元測試,那么可以做如下配置:

  • 繼續(xù)使用 Junit4 測試框架,包括其 @Test 注釋標(biāo)簽和相關(guān)的類和方法的定義,這些都不用變
  • 您需要通過 @RunWith(SpringJUnit4ClassRunner.class) 來啟動 Spring 對測試類的支持
  • 您需要通過 @ContextConfiguration 注釋標(biāo)簽來指定 Spring 配置文件或者配置類的位置
  • 您需要通過 @Transactional 來啟用自動的事務(wù)管理
  • 您可以使用 @Autowired 自動織入 Spring 的 bean 用來測試

另外您不再需要:

  • 手工加載 Spring 的配置文件
  • 手工清理數(shù)據(jù)庫的每次變更
  • 手工獲取 application context 然后獲取 bean 實(shí)例

Spring 測試注釋標(biāo)簽

我們已經(jīng)看到利用 Spring test framework 來進(jìn)行基于 Junit4 的單元測試是多么的簡單,下面我們來看一下前面遇到的各種注釋標(biāo)簽的一些可選用法。

@ContextConfiguration 和 @Configuration 的使用

剛才已經(jīng)介紹過,可以輸入 Spring xml 文件的位置,Spring test framework 會自動加載 XML 文件,得到 application context,當(dāng)然也可以使用 Spring 3.0 新提供的特性 @Configuration,這個注釋標(biāo)簽允許您用 Java 語言來定義 bean 實(shí)例,舉個例子:

現(xiàn)在我們將前面定義的 Spring-db1.xml 進(jìn)行修改,我們希望其中的三個 bean:initer、accountDao、accountService 通過配置類來定義,而不是 XML,則我們需要定義如下配置類:

注意:如果您想使用 @Configuration,請?jiān)?classpath 中加入 cglib 的 jar 包(cglib-nodep-2.2.3.jar),否則會報(bào)錯。


清單 8. SpringDb2Config.Java
 package config;  import org.Springframework.beans.factory.annotation.Autowired;  import org.Springframework.context.annotation.Bean;  import org.Springframework.context.annotation.Configuration;  import org.Springframework.jdbc.datasource.DriverManagerDataSource;  import service.AccountService;  import service.Initializer;  import DAO.AccountDao;  @Configuration  public class SpringDb2Config { 	 private @Autowired DriverManagerDataSource datasource; 	 @Bean 	 public Initializer initer() { 		 return new Initializer(); 	 } 		 @Bean 	 public AccountDao accountDao() {  AccountDao DAO = new AccountDao();  DAO.setDataSource(datasource);  return DAO; 	 } 		 @Bean 	 public AccountService accountService() {  return new AccountService(); 	 }  } 

注意上面的注釋標(biāo)簽:

  • @Configuration:表明這個類是一個 Spring 配置類,提供 Spring 的 bean 定義,實(shí)際效果等同于 XML 配置方法
  • @Bean:表明這個方法是一個 bean 的定義,缺省情況下,方法名稱就是 bean 的 Id
  • @Autowired:這個 datasource 采用自動注入的方式獲取

注意,我們采用的是 XML+config bean 的方式進(jìn)行配置,這種方式比較符合實(shí)際項(xiàng)目的情況。相關(guān)的 Spring 配置文件也要做變化,如下清單所示:


清單 9. Spring-db2.xml
 <beans xmlns="http://www.Springframework.org/schema/beans"	 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.Springframework.org/schema/context" 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">  <context:annotation-config/>  <bean id="datasource"  class="org.Springframework.jdbc.datasource.DriverManagerDataSource"> 		 <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> 		 <property name="url" value="jdbc:hsqldb:hsql://localhost" /> 		 <property name="username" value="sa"/> 		 <property name="password" value=""/> 	 </bean>  <bean id="transactionManager"           class="org.Springframework.jdbc.datasource.DataSourceTransactionManager">  		 <property name="dataSource" ref="datasource"></property>  	 </bean>  	 	 <bean class="config.SpringDb2Config"/>  </beans> 

注意里面的 context 命名空間的定義,如代碼中黑體字所示。另外還必須有 <context:annotaiton-config/> 的定義,這個定義允許采用注釋標(biāo)簽的方式來控制 Spring 的容器,最后我們看到 beans 已經(jīng)沒有 initer、accountDao 和 accountService 這些 bean 的定義,取而代之的是一個 SpringDb2Config bean 的定義,注意這個 bean 沒有名稱,因?yàn)椴恍枰灰谩?/p>

現(xiàn)在有了這些配置,我們的測試類只要稍稍修改一下,即可實(shí)現(xiàn)加載配置類的效果,如下:

 @ContextConfiguration("/config/Spring-db2.xml") 

通過上面的配置,測試用例就可以實(shí)現(xiàn)加載 Spring 配置類,運(yùn)行結(jié)果也是成功的 green bar。

@DirtiesContext

缺省情況下,Spring 測試框架一旦加載 applicationContext 后,將一直緩存,不會改變,但是,

由于 Spring 允許在運(yùn)行期修改 applicationContext 的定義,例如在運(yùn)行期獲取 applicationContext,然后調(diào)用 registerSingleton 方法來動態(tài)的注冊新的 bean,這樣的情況下,如果我們還使用 Spring 測試框架的被修改過 applicationContext,則會帶來測試問題,我們必須能夠在運(yùn)行期重新加載 applicationContext,這個時候,我們可以在測試類或者方法上注釋:@DirtiesContext,作用如下:

  • 如果定義在類上(缺?。?,則在此測試類運(yùn)行完成后,重新加載 applicationContext
  • 如果定義在方法上,即表示測試方法運(yùn)行完成后,重新加載 applicationContext

@TransactionConfiguration 和 @Rollback

缺省情況下,Spring 測試框架將事務(wù)管理委托到名為 transactionManager 的 bean 上,如果您的事務(wù)管理器不是這個名字,那需要指定 transactionManager 屬性名稱,還可以指定 defaultRollback 屬性,缺省為 true,即所有的方法都 rollback,您可以指定為 false,這樣,在一些需要 rollback 的方法,指定注釋標(biāo)簽 @Rollback(true)即可。

對 Junit4 的注釋標(biāo)簽支持

看了上面 Spring 測試框架的注釋標(biāo)簽,我們來看看一些常見的基于 Junit4 的注釋標(biāo)簽在 Spring 測試環(huán)境中的使用方法。

@Test(expected=...)

此注釋標(biāo)簽的含義是,這是一個測試,期待一個異常的發(fā)生,期待的異常通過 xxx.class 標(biāo)識。例如,我們修改 AccountService.Java 的 insertIfNotExist 方法,對于傳入的參數(shù)如果為空,則拋出 IllegalArgumentException,如下:

 public void insertIfNotExist(Account account) { 	 if(account==null) 		 throw new IllegalArgumentException("account is null"); 	 Account acct = accountDao.getAccountById(account.getId()); 	 if(acct==null) { 		 log.debug("No "+account+" found,would insert it."); 		 accountDao.saveAccount(account); 	 } 	 acct = null;  } 

然后,在測試類中增加一個測試異常的方法,如下:

 @Test(expected=IllegalArgumentException.class)  public void testInsertException() { 	 service.insertIfNotExist(null);  } 

運(yùn)行結(jié)果是 green bar。

@Test(timeout=...)

可以給測試方法指定超時時間(毫秒級別),當(dāng)測試方法的執(zhí)行時間超過此值,則失敗。

比如在 AccountService 中增加如下方法:

 public void doSomeHugeJob() { 	 try { 		 Thread.sleep(2*1000); 	 } catch (InterruptedException e) { 	 }  } 

上述方法模擬任務(wù)執(zhí)行時間 2 秒,則測試方法如下:

 @Test(timeout=3000)  public void testHugeJob() { 	 service.doSomeHugeJob();  } 

上述測試方法期待 service.doSomeHugeJob 方法能在 3 秒內(nèi)結(jié)束,執(zhí)行測試結(jié)果是 green bar。

@Repeat

通過 @Repeat,您可以輕松的多次執(zhí)行測試用例,而不用自己寫 for 循環(huán),使用方法:

 @Repeat(3)  @Test(expected=IllegalArgumentException.class)  public void testInsertException() { 	 service.insertIfNotExist(null);  } 

這樣,testInsertException 就能被執(zhí)行 3 次。

在測試類中基于 profile 加載測試 bean

從 Spring 3.2 以后,Spring 開始支持使用 @ActiveProfiles 來指定測試類加載的配置包,比如您的配置文件只有一個,但是需要兼容生產(chǎn)環(huán)境的配置和單元測試的配置,那么您可以使用 profile 的方式來定義 beans,如下:


清單 10. Spring-db.xml
 <beans xmlns="http://www.Springframework.org/schema/beans"	 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.Springframework.org/schema/beans  http://www.Springframework.org/schema/beans/Spring-beans-3.2.xsd">  	 <beans profile="test">  <bean id="datasource"  class="org.Springframework.jdbc.datasource.DriverManagerDataSource">  <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> 	 <property name="url" value="jdbc:hsqldb:hsql://localhost" /> 	 <property name="username" value="sa"/> 	 <property name="password" value=""/> 	 </bean> </beans> 	<beans profile="production">  <bean id="datasource"  class="org.Springframework.jdbc.datasource.DriverManagerDataSource"> 	 <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> 	 <property name="url" value="jdbc:hsqldb:hsql://localhost/prod" /> 	 <property name="username" value="sa"/> 	 <property name="password" value=""/> 	 </bean>  </beans>  <beans profile="test,production">  <bean id="transactionManager"      class="org.Springframework.jdbc.datasource.DataSourceTransactionManager"> 	 <property name="dataSource" ref="datasource"></property> 	 </bean> 	 <bean id="initer" init-method="init" class="service.Initializer"> 	 </bean>  <bean id="accountDao" depends-on="initer" class="DAO.AccountDao"> 	 		 <property name="dataSource" ref="datasource"/> 	 	 </bean> 	 		 	 <bean id="accountService" class="service.AccountService"> 	 	 </bean> 	 	 <bean id="envSetter" class="EnvSetter"/>  	 </beans>  </beans> 

上面的定義,我們看到:

  • 在 XML 頭中我們引用了 Spring 3.2 的 beans 定義,因?yàn)橹挥?Spring 3.2+ 才支持基于 profile 的定義
  • 在 <beans> 根節(jié)點(diǎn)下可以嵌套 <beans> 定義,要指定 profile 屬性,這個配置中,我們定義了兩個 datasource,一個屬于 test profile,一個輸入 production profile,這樣,我們就能在測試程序中加載 test profile,不影響 production 數(shù)據(jù)庫了
  • 在下面定義了一些屬于兩個 profile 的 beans,即 <beans profile=”test,production”> 這樣方便重用一些 bean 的定義,因?yàn)檫@些 bean 在兩個 profile 中都是一樣的

清單 11. AccountServiceTest.Java
 @RunWith(SpringJUnit4ClassRunner.class)  @ContextConfiguration("/config/Spring-db.xml")  @Transactional  @ActiveProfiles("test")  public class AccountServiceTest {  ...  } 

注意上面的 @ActiveProfiles,可以指定一個或者多個 profile,這樣我們的測試類就僅僅加載這些名字的 profile 中定義的 bean 實(shí)例。

對 TestNG 的支持

Spring 2.5 以后,就開始支持 TestNG 了,支持的方法包括:

  • 將您的 TestNG 測試類繼承 Spring 的測試父類:AbstractTransactionalTestNGSpringContextTests 或者 AbstractTestNGSpringContextTests,這樣您的 TestNG 測試類內(nèi)部就可以訪問 applicationContext 成員變量了
  • 不繼承 Spring 父類,在測試類上使用 @TestExecutionListeners 注釋標(biāo)簽,可以引入的監(jiān)聽器包括
    • DependencyInjectionTestExecutionListener:使得測試類擁有依賴注入特性
    • DirtiesContextTestExecutionListener:使得測試類擁有更新 applicationContext 能力
    • TransactionalTestExecutionListener:使得測試類擁有自動的事務(wù)管理能力

這里我們演示一下如何使用 Spring 提供的 TestNG 父類來進(jìn)行測試。


清單 12. AccountServiceTestNGTest.Java
 package testng;  import static org.Junit.Assert.assertEquals;  import org.Springframework.beans.factory.annotation.Autowired;  import org.Springframework.test.context.ActiveProfiles;  import org.Springframework.test.context.ContextConfiguration;  import org.Springframework.test.context.testng.  AbstractTransactionalTestNGSpringContextTests;  import org.Springframework.transaction.annotation.Transactional;  import service.AccountService;  import domain.Account;  @ContextConfiguration("/config/Spring-db.xml")  @Transactional  @ActiveProfiles("test")  public class AccountServiceTestNGTest extends  AbstractTransactionalTestNGSpringContextTests { 	 @Autowired 	 private AccountService service; 		 @org.testng.annotations.Test 	 public void testGetAcccountById() { 		 Account acct = Account.getAccount(1, "user01", 18, "M"); 		 service.insertIfNotExist(acct); 		 Account acct2 = service.getAccountById(1); 		 assertEquals(acct,acct2); 	 }  } 

執(zhí)行測試,我們將看到測試成功。


圖 4. 測試成功
 

搜索數(shù)據(jù)庫對應(yīng)的表,我們看到里面沒有數(shù)據(jù),說明自動事務(wù)起作用了。

基本原理

Spring test framework 主要位于 org.Springframework.test.context 包中,主要包括下面幾個類:


圖 5. Spring 測試框架類圖(查看大圖
 
  • TestContextManager:主要的入口類,提供 TestContext 實(shí)例的管理,負(fù)責(zé)根據(jù)各種事件來通知測試監(jiān)聽器
  • TestContext:實(shí)體類,提供訪問 Spring applicatoin context 的能力,并負(fù)責(zé)緩存 applicationContext
  • TestExecutionListener:測試監(jiān)聽器,提供依賴注入、applicationContext 緩存和事務(wù)管理等能力
  • ContextLoader:負(fù)責(zé)根據(jù)配置加載 Spring 的 bean 定義,以構(gòu)建 applicationContext 實(shí)例對象
  • SmartContextLoader:Spring 3.1 引入的新加載方法,支持按照 profile 加載

Spring 通過 AOP hook 了測試類的實(shí)例創(chuàng)建、beforeClass、before、after、afterClass 等事件入口,執(zhí)行順序主要如下:


圖 6. Spring 測試框架執(zhí)行序列圖(查看大圖
 
  • 測試執(zhí)行者開始執(zhí)行測試類,這個時候 Spring 獲取消息,自動創(chuàng)建 TestContextManager 實(shí)例
  • TestContextManager 會創(chuàng)建 TestContext,以記錄當(dāng)前測試的上下文信息,TestContext 則通過 ContextLoader 來獲取 Spring ApplicationContext 實(shí)例
  • 當(dāng)測試執(zhí)行者開始執(zhí)行測試類的 BeforeClass、Before、After、AfterClass 的時候,TestContextManager 將截獲事件,發(fā)通知給對應(yīng)的 TestExecutionListener

總結(jié)

根據(jù)上面的例子和介紹,我們可以看到,Spring 測試框架的主要特點(diǎn)如下:

  • 完美的支持了 Junit4(提供特別的 SpringJunit4ClassRunner),比較好的支持了 TestNG
  • 在支持原有單元測試能力的基礎(chǔ)上,通過各種監(jiān)聽器,支持了測試類的依賴注入、對 Spring applicationContext 的訪問以及事務(wù)管理能力,為使用 Spring 架構(gòu)的應(yīng)用程序的測試帶來了極大的便利性
  • Spring 3.1 引入的基于 profile 的加載能力使得測試環(huán)境和正式環(huán)境可以在一個 XML 定義中完美的結(jié)合

總之,如果您的程序中使用了 Spring,且對用 Junit 或者 testNG 來對他們進(jìn)行單元測試感到力不從心,可以考慮使用 Spring test framework,它將使您的應(yīng)用程序的質(zhì)量上一個新的臺階。

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Spring boot Mybatis(讀寫分離配置)
在spring配置jdbc
工作流Activiti的學(xué)習(xí)總結(jié)(四)Spring和Activiti的整合配置講解
(四)spring+servlet 整合
MyBatis3整合Spring3_SpringMVC
使用 Spring 2.5 TestContext 測試框架
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服