首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

使用 Spring 2.5 TestContext 测试框架(2)编写 UserService 的测试用例

使用 Spring 2.5 TestContext 测试框架(2)编写 UserService 的测试用例

编写 UserService 的测试用例下面我们为 UserService 编写一个简单的测试用例类,此时的目标是让这个基于 TestContext 测试框架的测试类运行起来,我们将在后面逐步完善这个测试用例。
清单3.TestUserService.java: 基于注解的测试用例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.baobaotao.service;

import org.springframework.test.context.junit4.
    AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.junit.Test;
import com.baobaotao.domain.User;

import java.util.Date;

@ContextConfiguration  //①
public class TestUserService extends
    AbstractTransactionalJUnit4SpringContextTests {
   
@Autowired  //②
   private UserService userService;

   @Test  //③
   public void handleUserLogin(){
       User user = new User();
       user.setUserId(1);
       user.setLastIp("127.0.0.1");
       Date now = new Date();
       user.setLastVisit(now.getTime());
       userService.handleUserLogin(user);
   }
}




这里,我们让 TestUserService 直接继承于 Spring 所提供的 AbstractTransactionalJUnit4SpringContextTests 的抽象测试类,稍后本文将对这个抽象测试类进行剖析,这里你仅须知道该抽象测试类的作用是让 TestContext 测试框架可以在 JUnit 4.4 测试框架基础上运行起来就可以了。
在 ① 处,标注了一个类级的 @ContextConfiguration 注解,这里 Spring 将按 TestContext 契约查找 classpath:/com/baobaotao/service/TestUserService-context.xml 的 Spring 配置文件,并使用该配置文件启动 Spring 容器。@ContextConfiguration 注解有以下两个常用的属性:
  • locations:可以通过该属性手工指定 Spring 配置文件所在的位置,可以指定一个或多个 Spring 配置文件。如下所示:
    @ContextConfiguration(locations={“xx/yy/beans1.xml”,” xx/yy/beans2.xml”})
  • inheritLocations:是否要继承父测试用例类中的 Spring 配置文件,默认为 true。如下面的例子:
    1
    2
    3
    4
    5
    6
    7
    8
    @ContextConfiguration(locations={"base-context.xml"})
    public class BaseTest {
         // ...
    }
    @ContextConfiguration(locations={"extended-context.xml"})
    public class ExtendedTest extends BaseTest {
         // ...
    }




如果 inheritLocations 设置为 false,则 ExtendedTest 仅会使用 extended-context.xml 配置文件,否则将使用 base-context.xml 和 extended-context.xml 这两个配置文件。
② 处的 @Autowired 注解让 Spring 容器自动注入 UserService 类型的 Bean。而在 ③ 处标注的 @Test 注解则让 handleUserLogin() 方法成为一个 JUnit 4.4 标准的测试方法, @Test 是 JUnit 4.4 所定义的注解。
在运行 TestUserService 测试类之前,让我们先看一下 TestUserService-context.xml 配置文件的内容:
清单 4.TestUserService 所引用的 Spring 配置文件
1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8" ?>
<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-2.5.xsd">

<!-- ① 引入清单1定义的Spring配置文件 -->
<import resource="classpath:/applicationContext.xml"/>

</beans>




在 ① 处引入了清单 1 中定义的 Spring 配置文件,这样我们就可以将其中定义的 UserService Bean 作为测试固件注入到 TestUserService 中了。
在你的 IDE 中(Eclipse、JBuilder、Idea 等),将 JUnit 4.4 类包引入到项目工程中后,在 TestUserService 类中点击右键运行该测试类,将发现 TestUserService 已经可以成功运行了,如 图 1 所示:
图 1. 在 Eclipse 6.0 中运行 TestUserServiceTestUserService 可以正确运行,说明其 userService 这个测试固件已经享受了 Spring 自动注入的功能。在运行该测试用例后,到数据库中查看 t_user 表和 t_login_log 表,你会发现表数据和测试前是一样的!这说明虽然我们在清单 3 的 handleUserLogin() 测试方法中执行了 userService.handleUserLogin(user) 的操作,但它并没有对数据库现场造成破坏:这是因为 Spring 的在测试方法返回前进行了事务回滚操作。
虽然 TestUserService.handleUserLogin() 测试方法已经可以成功运行,但是它在测试功能上是不完善的,读者朋友可以已经发现了它存在以下两个问题:
  • 我们仅仅执行了 UserService#handleUserLogin(user) 方法,但验证该方法执行结果的正确性。
  • 在测试方法中直接使用 ID 为 1 的 User 对象进行测试,这相当于要求在数据库 t_user 表必须已经存在 ID 为 1 的记录,如果 t_user 中不存在这条记录,将导致测试方法执行失败。
返回列表