Junit 之 与Spring集成

使用 Spring 测试注释来进行常见的 Junit4 或者 TestNG 的单元测试,同时支持访问 Spring 的 beanFactory 和进行自动化的事务管理。
一、spring测试注解标签
1. @ContextConfiguration 和 @Configuration 的使用

Spring 3.0 新提供的特性 @Configuration,这个注释标签允许您用 Java 语言来定义 bean 实例。

 
  1. package config;   
  2.   
  3. import org.Springframework.beans.factory.annotation.Autowired;   
  4. import org.Springframework.context.annotation.Bean;   
  5. import org.Springframework.context.annotation.Configuration;   
  6. import org.Springframework.jdbc.datasource.DriverManagerDataSource;   
  7.   
  8. import service.AccountService;   
  9. import service.Initializer;   
  10. import DAO.AccountDao;   
  11.   
  12. @Configuration   
  13. public class SpringDb2Config {   
  14.  private @Autowired DriverManagerDataSource datasource;   
  15.  @Bean   
  16.  public Initializer initer() {   
  17.      return new Initializer();   
  18.  }   
  19.   
  20.  @Bean   
  21.  public AccountDao accountDao() {   
  22. AccountDao DAO = new AccountDao();   
  23. DAO.setDataSource(datasource);   
  24. return DAO;   
  25.  }   
  26.   
  27.  @Bean   
  28.  public AccountService accountService() {   
  29. return new AccountService();   
  30.  }   
  31. }  

通过@ContextConfiguration指定配置文件,Spring test framework 会自动加载 XML 文件,也是我们采用的方式。


2.@DirtiesContext
缺省情况下,Spring 测试框架一旦加载 applicationContext 后,将一直缓存,不会改变,但是,
由于 Spring 允许在运行期修改 applicationContext 的定义,例如在运行期获取 applicationContext,然后调用 registerSingleton 方法来动态的注册新的 bean,这样的情况下,如果我们还使用 Spring 测试框架的被修改过 applicationContext,则会带来测试问题,我们必须能够在运行期重新加载 applicationContext,这个时候,我们可以在测试类或者方法上注释:@DirtiesContext,作用如下:
如果定义在类上(缺省),则在此测试类运行完成后,重新加载 applicationContext
如果定义在方法上,即表示测试方法运行完成后,重新加载 applicationContext


3.@Transactional、@TransactionConfiguration 和 @Rollback
缺省情况下,Spring 测试框架将事务管理委托到名为 transactionManager 的 bean 上,如果您的事务管理器不是这个名字,那需要指定 transactionManager 属性名称,还可以指定 defaultRollback 属性,缺省为 true,即所有的方法都 rollback,您可以指定为 false,这样,在一些需要 rollback 的方法,指定注释标签 @Rollback(true)即可。事务的注解可以具体到方法


4.@Repeat
通过 @Repeat,您可以轻松的多次执行测试用例,而不用自己写 for 循环,使用方法:
 @Repeat(3) 
 @Test(expected=IllegalArgumentException.class) 
 public void testInsertException() { 
service.insertIfNotExist(null); 
 }
这样,testInsertException 就能被执行 3 次。


5.@ActiveProfiles 
从 Spring 3.2 以后,Spring 开始支持使用 @ActiveProfiles 来指定测试类加载的配置包,比如您的配置文件只有一个,但是需要兼容生产环境的配置和单元测试的配置,那么您可以使用 profile 的方式来定义 beans,如下: 

 
  1. <beans xmlns="http://www.Springframework.org/schema/beans"  
  2.      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.  xsi:schemaLocation="http://www.Springframework.org/schema/beans   
  4.  http://www.Springframework.org/schema/beans/Spring-beans-3.2.xsd">   
  5.      <beans profile="test">   
  6.  <bean id="datasource"   
  7.  class="org.Springframework.jdbc.datasource.DriverManagerDataSource">   
  8.  <property name="driverClassName" value="org.hsqldb.jdbcDriver" />   
  9.      <property name="url" value="jdbc:hsqldb:hsql://localhost" />   
  10.      <property name="username" value="sa"/>   
  11.      <property name="password" value=""/>   
  12.      </bean>   
  13. </beans>   
  14.       
  15. <beans profile="production">   
  16.  <bean id="datasource"   
  17.  class="org.Springframework.jdbc.datasource.DriverManagerDataSource">   
  18.      <property name="driverClassName" value="org.hsqldb.jdbcDriver" />   
  19.      <property name="url" value="jdbc:hsqldb:hsql://localhost/prod" />   
  20.      <property name="username" value="sa"/>   
  21.      <property name="password" value=""/>   
  22.      </bean>   
  23.  </beans>   
  24.  <beans profile="test,production">   
  25.  <bean id="transactionManager"   
  26.      class="org.Springframework.jdbc.datasource.DataSourceTransactionManager">   
  27.      <property name="dataSource" ref="datasource"></property>   
  28.      </bean>   
  29.      <bean id="initer" init-method="init" class="service.Initializer">   
  30.      </bean>   
  31.  <bean id="accountDao" depends-on="initer" class="DAO.AccountDao">   
  32.              <property name="dataSource" ref="datasource"/>   
  33.          </bean>   
  34.           
  35.          <bean id="accountService" class="service.AccountService">   
  36.          </bean>   
  37.          <bean id="envSetter" class="EnvSetter"/>   
  38.      </beans>   
  39.  </beans>  

上面的定义,我们看到:
在 XML 头中我们引用了 Spring 3.2 的 beans 定义,因为只有 Spring 3.2+ 才支持基于 profile 的定义
在 <beans> 根节点下可以嵌套 <beans> 定义,要指定 profile 属性,这个配置中,我们定义了两个 datasource,一个属于 test profile,一个输入 production profile,这样,我们就能在测试程序中加载 test profile,不影响 production 数据库了
在下面定义了一些属于两个 profile 的 beans,即 <beans profile=”test,production”> 这样方便重用一些 bean 的定义,因为这些 bean 在两个 profile 中都是一样的

  1. @RunWith(SpringJUnit4ClassRunner.class)   
  2.  @ContextConfiguration("/config/Spring-db.xml")   
  3.  @Transactional   
  4.  @ActiveProfiles("test")   
  5.  public class AccountServiceTest {   
  6.  ...   
  7.  }  

注意上面的 @ActiveProfiles,可以指定一个或者多个 profile,这样我们的测试类就仅仅加载这些名字的 profile 中定义的 bean 实例。

下面看一个配置实例:

添加依赖:

 
  1. <dependency>  
  2.             <groupId>junit</groupId>  
  3.             <artifactId>junit</artifactId>  
  4.             <version>4.10</version>  
  5.         </dependency>  
  6.         <dependency>  
  7.             <groupId>org.springframework</groupId>  
  8.             <artifactId>spring-test</artifactId>  
  9.             <version>4.0.1.RELEASE</version>  
  10.         </dependency>  
  11.         <dependency>  
  12.             <groupId>org.springframework</groupId>  
  13.             <artifactId>spring-context</artifactId>  
  14.             <version>4.0.1.RELEASE</version>  
  15.         </dependency>  
  16.         <dependency>  
  17.             <groupId>org.springframework</groupId>  
  18.             <artifactId>spring-tx</artifactId>  
  19.             <version>4.0.1.RELEASE</version>  
  20.         </dependency>  

spring配置:applicationContext.xml

 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  7.                         http://www.springframework.org/schema/context   
  8.                         http://www.springframework.org/schema/context/spring-context-3.0.xsd"  
  9.        default-lazy-init="false">  
  10.   
  11.     <description>Spring公共配置 </description>  
  12.     <context:component-scan base-package="cn.slimsmart.unit.test.demo" />  
  13. </beans>  

AddServiceImpl添加@service注解

测试类:

 
    1. package cn.slimsmart.unit.test.demo.junit;  
    2.   
    3. import static org.junit.Assert.assertTrue;  
    4.   
    5. import org.junit.After;  
    6. import org.junit.Before;  
    7. import org.junit.Test;  
    8. import org.junit.runner.RunWith;  
    9. import org.springframework.beans.factory.annotation.Autowired;  
    10. import org.springframework.test.context.ActiveProfiles;  
    11. import org.springframework.test.context.ContextConfiguration;  
    12. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
    13. import org.springframework.test.context.transaction.TransactionConfiguration;  
    14. import org.springframework.transaction.annotation.Transactional;  
    15.   
    16. //用来说明此测试类的运行者,这里用了 SpringJUnit4ClassRunner  
    17. @RunWith(SpringJUnit4ClassRunner.class)   
    18. //指定 Spring 配置信息的来源,支持指定 XML 文件位置或者 Spring 配置类名  
    19. @ContextConfiguration(locations = { "classpath*:/applicationContext.xml" })  
    20. //表明此测试类的事务启用,这样所有的测试方案都会自动的 rollback,  
    21. //@Transactional  
    22. //defaultRollback,是否回滚,默认为true  
    23. //transactionManager:指定事务管理器一般在spring配置文件里面配置  
    24. //@TransactionConfiguration(defaultRollback=true,transactionManager="transactionManager")  
    25. //但是需要兼容生产环境的配置和单元测试的配置,那么您可以使用 profile 的方式来定义 beans,  
    26. //@ActiveProfiles("test")  
    27. public class JunitSpringTest {  
    28.       
    29.     @Autowired  
    30.     private AddService addService;  
    31.       
    32.     @Before  
    33.     public void setUp(){  
    34.         System.out.println("初始化");  
    35.     }  
    36.       
    37.     @Test  
    38.     public void testAdd() {  
    39.         assertTrue(addService.add(1, 1) == 2);  
    40.     }  
    41.       
    42.     @After  
    43.     public void destroy() {  
    44.         System.out.println("退出,资源释放");  
    45.     }  
    46. }  
原文地址:https://www.cnblogs.com/rickzhai/p/7989077.html