使用Spring进行单元测试(下)

发表于:2013-6-07 10:48

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:赵才文    来源:51Testing软件测试网采编

  在测试类中基于 profile 加载测试 bean

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

  清单 10. Spring-db.xml

  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.    
  5. http://www.Springframework.org/schema/beans/Spring-beans-3.2.xsd"> 
  6.    
  7.      <beans profile="test"> 
  8.  <bean id="datasource" 
  9. > 
  10.  <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> 
  11.      <property name="url" value="jdbc:hsqldb:hsql://localhost" /> 
  12.      <property name="username" value="sa"/> 
  13.      <property name="password" value=""/> 
  14.      </bean> 
  15. </beans> 
  16.    
  17. <beans profile="production"> 
  18.  <bean id="datasource" 
  19. > 
  20.      <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> 
  21.      <property name="url" value="jdbc:hsqldb:hsql://localhost/prod" /> 
  22.      <property name="username" value="sa"/> 
  23.      <property name="password" value=""/> 
  24.      </bean> 
  25.  </beans> 
  26.  <beans profile="test,production"> 
  27.  <bean id="transactionManager" 
  28.     > 
  29.      <property name="dataSource" ref="datasource"></property> 
  30.      </bean> 
  31.      <bean id="initer" init-method="init"> 
  32.      </bean> 
  33.  <bean id="accountDao" depends-on="initer"> 
  34.              <property name="dataSource" ref="datasource"/> 
  35.          </bean> 
  36.    
  37.          <bean id="accountService"> 
  38.          </bean> 
  39.          <bean id="envSetter"/> 
  40.      </beans> 
  41.  </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 中都是一样的

  清单 11. AccountServiceTest.Java

  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 实例。

  对 TestNG 的支持

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

  ● 将您的 TestNG 测试类继承 Spring 的测试父类:AbstractTransactionalTestNGSpringContextTests 或者 AbstractTestNGSpringContextTests,这样您的 TestNG 测试类内部就可以访问 applicationContext 成员变量了

  ● 不继承 Spring 父类,在测试类上使用 @TestExecutionListeners 注释标签,可以引入的监听器包括

  → DependencyInjectionTestExecutionListener:使得测试类拥有依赖注入特性

  → DirtiesContextTestExecutionListener:使得测试类拥有更新 applicationContext 能力

  → TransactionalTestExecutionListener:使得测试类拥有自动的事务管理能力

  这里我们演示一下如何使用 Spring 提供的 TestNG 父类来进行测试。

  清单 12. AccountServiceTestNGTest.Java

  1. package testng;  
  2.    
  3. import static org.Junit.Assert.assertEquals;  
  4.    
  5. import org.Springframework.beans.factory.annotation.Autowired;  
  6. import org.Springframework.test.context.ActiveProfiles;  
  7. import org.Springframework.test.context.ContextConfiguration;  
  8. import org.Springframework.test.context.testng.  
  9. AbstractTransactionalTestNGSpringContextTests;  
  10. import org.Springframework.transaction.annotation.Transactional;  
  11.    
  12. import service.AccountService;  
  13. import domain.Account;  
  14.    
  15. @ContextConfiguration("/config/Spring-db.xml")  
  16. @Transactional 
  17. @ActiveProfiles("test")  
  18. public class AccountServiceTestNGTest extends 
  19. AbstractTransactionalTestNGSpringContextTests {  
  20.     @Autowired 
  21.     private AccountService service;  
  22.    
  23.     @org.testng.annotations.Test  
  24.     public void testGetAcccountById() {  
  25.         Account acct = Account.getAccount(1"user01"18"M");  
  26.         service.insertIfNotExist(acct);  
  27.         Account acct2 = service.getAccountById(1);  
  28.         assertEquals(acct,acct2);  
  29.     }  
  30. }

43/4<1234>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号