SpringMVC+Spring+MyBatis 的综合练习 10 (使用 Spring 测试 DAO)

10.1 测试的准备

Spring 提供了自己的模拟测试方法,可以自动注入需要的组件,也可以模拟页面的请求并返回结果,从而完成从 DAO 到 MVC 的多层面测试。要想使用 Spring 提供的测试方法,需要在 POM 中导入 spring-test 依赖。

<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.13.RELEASE</version>
    <scope>test</scope>
</dependency>

10.2 测试 DAO

以 Department 为例,编写测试方法。代码如下:

package com.hh.ssm.test;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.hh.ssm.bean.Department;
import com.hh.ssm.bean.Employee;
import com.hh.ssm.dao.DepartmentMapper;

/**
 * 测试 DepartmentDAO
 * @author HH
 * 对 Spring 项目推荐采用 Spring 单元测试,可以自动注入我们所需要的组件。
 * 1. 导入 Spring Test 模块。
 * 2. 使用 @ContextConfiguration 指定 Spring 配置文件的位置。
 * 3. 直接 @Autowired 我们要使用的组件
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class DepartmentTest {

    @Autowired
    DepartmentMapper deparmentMapper;

    @Test
    public void testInsertDepartment() {
        // 测试是否自动注入了组件
        System.out.println(deparmentMapper);
        // 1. 插入新的部门
        Department record = new Department();
        Date currentDate = new Date();
        String departmentName = "董事会";
        record.setDepartmentName(departmentName);
        record.setGmtCreate(currentDate);
        record.setGmtModified(currentDate);
        System.out.println(record);
        int result = deparmentMapper.insert(record);
        System.out.println("result = " + result);
    }

    @Test
    public void testSelectByPrimaryKey() {      
        Integer id = 5;     
        Department result = new Department();
        result = deparmentMapper.selectByPrimaryKey(id);
        System.out.println("result = " + result);       
    }

    @Test
    public void testSelectByPrimaryKeyWithEmployees() {     
        Integer id = 5;     
        Department result = new Department();
        result = deparmentMapper.selectByPrimaryKeyWithEmployees(id);
        System.out.println("result = " + result);
        System.out.println("================================");
        List<Employee> employees = result.getEmployees();
        System.out.println("employees : " + employees);
    }

    @Test
    public void testSelectByExampleWithNull() {
        int i=0;
        List<Department> result =  deparmentMapper.selectByExample(null);
        for (Department department : result) {
            i++;
            System.out.println(i + " " + department);
        }
    }
}

这些测试都是通过自动注入了 DepartmentMapper 组件后,调用了对应的接口实现的 CRUD 一系列动作。通过这些测试,可以让我们更好的理解 DAO 中各个接口的用法,同时也可以测试自己新增的接口(同时验证 xxxMapper.xml 的配置正确性)。

到目前为止,两个类的 CRUD 大体上搭建好了,下一步应该考虑的是 MVC 了。打算从前台页面开始,然后逐层编写实现MVC。

猜你喜欢

转载自blog.csdn.net/hh680821/article/details/79189357