实战SSM_O2O商铺_05集成SSM后验证DAO层、Service层、Controller层的配置

概述

整合SSM框架之后,我们分别对持久层、业务层、控制层分别做下单元测试。

Area这个实体类属性相对较少,我们就选择使用Area来测试吧。

首先为tb_area表准备2条数据

-- ----------------------------
-- Records of tb_area
-- ----------------------------
INSERT INTO `tb_area` VALUES ('1', '北京', '帝都', '0', '2018-05-13 21:00:26', '2018-05-14 21:00:33');
INSERT INTO `tb_area` VALUES ('2', '上海', '魔都', '99', '2018-05-13 21:00:36', '2018-05-14 21:00:41');

验证DAO层的配置

这里写图片描述

接口

结合/o2o/src/main/resources/spring/spring-dao.xml中的配置
这里写图片描述

所以 接口层的类都应该写在com.artisan.o2o.dao包下

AreaDao.java

package com.artisan.o2o.dao;

import java.util.List;

import com.artisan.o2o.entity.Area;

public interface AreaDao {

    List<Area> queryArea();
}

SQL映射文件

结合/o2o/src/main/resources/spring/spring-dao.xml中的配置

这里写图片描述

所以在/o2o/src/main/resources/mapper目录下 新建映mybatis的SQL映射文件

AreaDao.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.artisan.o2o.dao.AreaDao">
    <select id="queryArea" resultType="Area">
        SELECT
            area_id,
            area_name,
            area_desc,
            priority,
            create_time,
            last_edit_time
        FROM
            tb_area
        ORDER BY
            priority DESC
    </select>
</mapper>   

单元测试类

src/test/java目录下新建com.artisan.o2o包,使用spring-test更加方便的进行单元测试。 我们已经在 pom.xml中添加了spring-test的依赖。

创建 Spring Test 的基类,该类主要用来加载配置文件,设置web环境。所有的测试类,都需要继承该类初始化spring信息。

基类

放在com.artisan.o2o目录下,所有的测试类,都需要继承该类初始化spring信息。

package com.artisan.o2o;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * 
 * 
 * @ClassName: BaseTest
 * 
 * @Description: 测试类的基类,配置Spring和junit整合,junit启动时加载springIOC容器
 * 
 * @author: Mr.Yang
 * 
 * @date: 2018年5月14日 下午12:58:21
 */

@RunWith(SpringJUnit4ClassRunner.class)
// 告诉junit spring配置文件
@ContextConfiguration({ "classpath:spring/spring-dao.xml", "classpath:spring-service.xml" })
public class BaseTest {

}

DAO层单元测试类

com.artisan.o2o.dao包下建立DAO层的测试类,继承BaseTest

package com.artisan.o2o.dao;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.artisan.o2o.BaseTest;
import com.artisan.o2o.entity.Area;

/**
 * 
 * 
 * @ClassName: AreaDaoTest
 * 
 * @Description: 集成SSM后对DAO层进行验证
 * 
 * @author: Mr.Yang
 * 
 * @date: 2018年5月14日 下午5:21:49
 */
public class AreaDaoTest extends BaseTest {

    @Autowired
    AreaDao areaDao;

    @Test
    public void testQueryArea() {
        List<Area> areaList = areaDao.queryArea();
        // 插入了2条测试数据,期望list中有2条
        assertEquals(2, areaList.size());
        // SQL中按照权重排序,上海priority 99 ,期望第一条数据是 上海

        System.out.println(areaList.get(0));

        assertEquals("上海", areaList.get(0).getAreaName());
    }
}

单元测试运行正常。

关键日志

JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@a1f72f5] will not be managed by Spring
==>  Preparing: SELECT area_id, area_name, area_desc, priority, create_time, last_edit_time FROM tb_area ORDER BY priority DESC 
==> Parameters: 
<==    Columns: area_id, area_name, area_desc, priority, create_time, last_edit_time
<==        Row: 2, 上海, 魔都, 99, 2018-05-13 21:00:36.0, 2018-05-14 21:00:41.0
<==        Row: 1, 北京, 帝都, 0, 2018-05-13 21:00:26.0, 2018-05-14 21:00:33.0
<==      Total: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@61710c6]
Area [areaId=2, areaName=上海, areaDesc=魔都, priority=99, createTime=Sun May 13 21:00:36 BOT 2018, lastEditTime=Mon May 14 21:00:41 BOT 2018]

验证Service层的配置

这里写图片描述

接口以及实现类

接口

package com.artisan.o2o.service;

import java.util.List;

import com.artisan.o2o.entity.Area;

public interface AreaService {
    /**
     * 
     * 
     * @Title: getAreaList
     * 
     * @Description: 获取区域列表
     * 
     * @return
     * 
     * @return: List<Area>
     */
    List<Area> getAreaList();
}

实现类

package com.artisan.o2o.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.artisan.o2o.dao.AreaDao;
import com.artisan.o2o.entity.Area;
import com.artisan.o2o.service.AreaService;

/**
 * 
 * 
 * @ClassName: AreaServiceImpl
 * 
 * @Description: @Service标注的服务层
 * 
 * @author: Mr.Yang
 * 
 * @date: 2018年5月14日 下午9:06:45
 */
@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    AreaDao areaDao;

    @Override
    public List<Area> getAreaList() {
        return areaDao.queryArea();
    }

}

单元测试类

package com.artisan.o2o.service;

import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.artisan.o2o.BaseTest;
import com.artisan.o2o.entity.Area;

public class AreaServiceTest extends BaseTest {

    @Autowired
    AreaService areaService;

    @Test
    public void testGetAreaList() {
        List<Area> areaList = areaService.getAreaList();
        Assert.assertEquals("上海", areaList.get(0).getAreaName());
    }
}

BaseTest基类中,别忘了加载classpath:spring-service.xml 配置文件,否则报错。 第一步已经加好了,这里就不重复了。

单元测试运行正常。

关键日志

JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@a1f72f5] will not be managed by Spring
==>  Preparing: SELECT area_id, area_name, area_desc, priority, create_time, last_edit_time FROM tb_area ORDER BY priority DESC 
==> Parameters: 
<==    Columns: area_id, area_name, area_desc, priority, create_time, last_edit_time
<==        Row: 2, 上海, 魔都, 99, 2018-05-13 21:00:36.0, 2018-05-14 21:00:41.0
<==        Row: 1, 北京, 帝都, 0, 2018-05-13 21:00:26.0, 2018-05-14 21:00:33.0
<==      Total: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@61710c6]
Area [areaId=2, areaName=上海, areaDesc=魔都, priority=99, createTime=Sun May 13 21:00:36 BOT 2018, lastEditTime=Mon May 14 21:00:41 BOT 2018]

验证Controller层的配置

这里写图片描述

控制层

package com.artisan.o2o.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.artisan.o2o.entity.Area;
import com.artisan.o2o.service.AreaService;

@Controller
@RequestMapping("/superadmin")
public class AreaController {

    @Autowired
    AreaService areaService;

    @RequestMapping(value = "/listArea", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getAreas() {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Area> areaList = new ArrayList<Area>();
        try {
            areaList = areaService.getAreaList();
            map.put("total", areaList.size());
            map.put("rows", areaList);

            for (Area area : areaList) {
                System.out.println("区域:" + area);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("errMsg", e.getMessage().toString());
        }
        return map;
    }

}

启动服务,浏览器访问

访问如下地址:
http://localhost:8080/o2o/superadmin/listArea

为了美化JSON,在浏览器中,更直观的查看JSON,在Chrome中安装了json-handle插件,展示效果如下:

这里写图片描述


附- 模拟HTTP 请求调测利器Postman

Postman概述

在开发或者调试网络程序或者是网页B/S模式的程序的时候,常常需要跟踪监视网页HTTP请求,测试服务器响应是否正确。

Chrome 浏览器扩展插件:Postman,可以模拟用户 HTTP 请求的数据发送到服务器,方便测试服务器接口。

  • (1)Postman 是一个 Chrome 扩展,提供功能强大的 API & HTTP 请求调试。
  • (2)能够发送任何类型的 HTTP requests(GET, HEAD, POST, PUT..),附带任何数量的参数,以及自定义Http 头信息(HTTP Headers)。
  • (3)支持不同的认证机制(basic, digest, OAuth),接收到的响应语法高亮(HTML,JSON 或 XML)。
  • (4)能够保留了历史的请求,这样我们就可以很容易地重新发送请求,有一个“集合”功能,用于存储所有请求相同的API/域。
  • (5)项目地址:https://www.getpostman.com/

Postman安装使用


使用Postman

浏览器中输入 chrome://apps/ , 找到并打开Postman

这里写图片描述

猜你喜欢

转载自blog.csdn.net/yangshangwei/article/details/80320081