Spring 测试注释标签

  我们已经看到利用 Spring test framework 来进行基于 Junit4 的单元测试是多么的简单,下面我们来看一下前面遇到的各种注释标签的一些可选用法。

  @ContextConfiguration 和 @Configuration 的使用

  刚才已经介绍过,可以输入 Spring xml 文件的位置,Spring test framework 会自动加载 XML 文件,得到 application context,当然也可以使用 Spring 3.0 新提供的特性 @Configuration,这个注释标签允许您用 Java 语言来定义 bean 实例,举个例子:

  现在我们将前面定义的 Spring-db1.xml 进行修改,我们希望其中的三个 bean:initer、accountDao、accountService 通过配置类来定义,而不是 XML,则我们需要定义如下配置类:

  注意:如果您想使用 @Configuration,请在 classpath 中加入 cglib 的 jar 包(cglib-nodep-2.2.3.jar),否则会报错。

  清单 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(); 
        } 
    }

  注意上面的注释标签:

  ● @Configuration:表明这个类是一个 Spring 配置类,提供 Spring 的 bean 定义,实际效果等同于 XML 配置方法

  ● @Bean:表明这个方法是一个 bean 的定义,缺省情况下,方法名称是 bean 的 Id

  ● @Autowired:这个 datasource 采用自动注入的方式获取

  注意,我们采用的是 XML+config bean 的方式进行配置,这种方式比较符合实际项目的情况。相关的 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"
    >
             <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"
             >
             <property name="dataSource" ref="datasource"></property>
         </bean>
      
         <bean/>
     </beans>

  注意里面的 context 命名空间的定义,如代码中黑体字所示。另外还必须有 <context:annotaiton-config/> 的定义,这个定义允许采用注释标签的方式来控制 Spring 的容器,后我们看到 beans 已经没有 initer、accountDao 和 accountService 这些 bean 的定义,取而代之的是一个 SpringDb2Config bean 的定义,注意这个 bean 没有名称,因为不需要被引用。

  现在有了这些配置,我们的测试类只要稍稍修改一下,即可实现加载配置类的效果,如下:

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

  通过上面的配置,测试用例可以实现加载 Spring 配置类,运行结果也是成功的 green bar。

  @DirtiesContext

  缺省情况下,Spring 测试框架一旦加载 applicationContext 后,将一直缓存,不会改变,但是,

  由于 Spring 允许在运行期修改 applicationContext 的定义,例如在运行期获取 applicationContext,然后调用 registerSingleton 方法来动态的注册新的 bean,这样的情况下,如果我们还使用 Spring 测试框架的被修改过 applicationContext,则会带来测试问题,我们必须能够在运行期重新加载 applicationContext,这个时候,我们可以在测试类或者方法上注释:@DirtiesContext,作用如下:

  ● 如果定义在类上(缺省),则在此测试类运行完成后,重新加载 applicationContext

  ● 如果定义在方法上,即表示测试方法运行完成后,重新加载 applicationContext