Board logo

标题: 使用 Spring 2.5 TestContext 测试框架(2)编写 UserService 的测试用例 [打印本页]

作者: look_w    时间: 2018-8-9 09:38     标题: 使用 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 注解有以下两个常用的属性:
如果 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() 测试方法已经可以成功运行,但是它在测试功能上是不完善的,读者朋友可以已经发现了它存在以下两个问题:





欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0