博客
关于我
springboot集成spock,groovy单元测试框架
阅读量:399 次
发布时间:2019-03-05

本文共 2761 字,大约阅读时间需要 9 分钟。

单元测试环境配置与实践

在进行单元测试时,最好避免与实际数据库耦合,以确保测试在任何环境下都能顺利通过。本文将详细介绍如何通过h2内存数据库、liquibase初始化测试数据以及embedded-redis等技术手段,打破环境依赖。

技术框架与环境

本项目主要采用以下技术和工具:

  • Spring Boot: 作为快速开发框架
  • IDE: Idea
  • 构建工具: Maven
  • 数据库迁移工具: Liquibase
  • 测试框架: Spock
  • 依赖管理工具: Cglib-nodep
  • Redis: 通过embedded-redis实现内存缓存
  • ORM工具: MyBatis-Plus

项目结构

项目采用模块化设计,主要包含以下部分:

  • Application: 业务逻辑核心
  • Domain: 数据层面模型
  • Application-Test: 测试模块
  • Config: 配置文件
  • Mapper: 数据映射接口
  • Service: 业务服务逻辑

POM.xml配置说明

项目依赖管理文件(POM.xml)中定义了以下关键库的版本:

  • Liquibase: 3.8.9
  • H2数据库: 1.4.197
  • Groovy语言: 2.5.14
  • Spock测试框架: 1.3-groovy-2.5
  • Cglib-nodep: 2.2
  • Junit-Platform-Launcher: 1.7.1
  • Embedded-Redis: 0.7.3

application-test.yml配置

测试环境配置文件(application-test.yml)中定义了以下关键参数:

  • 服务器端口: 8000
  • 数据库配置: 使用Druid数据源,配置了初始连接池大小、最小空闲连接数、最大活跃连接数等
  • Redis配置: 配置了数据库编号、主机地址、连接池参数等
  • MyBatis-Plus配置: 开启了自动驼峰命名规则、全自动映射功能
  • Swagger配置: 启用Swagger 3.0文档生成

Liquibase配置

Liquibase配置文件中主要定义了以下内容:

  • 数据库变更日志: 指向classpath下的数据库变更文件
  • 锁表管理: 定义了锁表的名称和自动锁表管理策略
  • 数据库变更表: 设置了默认的变更表名
  • 滚回测试: 禁用了在测试中自动滚回变更

测试配置

Redis测试配置

@Configuration@ConditionalOnProperty(prefix = "test.conditional", name = "enabled", havingValue = "true")public class TestRedisConfig {    @Bean    public LettuceConnectionFactory redisConnectionFactory(RedisProperties redisProperties) {        return new LettuceConnectionFactory(redisProperties.getHost(), redisProperties.getPort());    }    @Bean    public RedisTemplate
redisTemplate(LettuceConnectionFactory connectionFactory) { RedisTemplate
template = new RedisTemplate<>(); template.setConnectionFactory(connectionFactory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new StringRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class)); return template; }}

Redis服务测试配置

@Configuration@ConditionalOnProperty(prefix = "test.conditional", name = "enabled", havingValue = "true")public class TestRedisServiceConfig {    private RedisServer redisServer;    public TestRedisServiceConfig(RedisProperties redisProperties) {        this.redisServer = RedisServer.builder().port(redisProperties.getPort())                .setting("maxmemory 128M")                .build();    }    @PostConstruct    public void postConstruct() {        redisServer.start();    }    @PreDestroy    public void preDestroy() {        redisServer.stop();    }}

测试用例示例

@SpringBootTest(classes = FreedomApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)@ActiveProfiles("test")@Stepwisepublicpublic interface CodeBootTest {}

测试报告

通过执行 mvn test 命令,可以获取以下测试报告:

  • 测试覆盖率报告: 展示各测试类的覆盖情况
  • 测试执行日志: 包括测试用例执行结果、错误信息等
  • 代码覆盖率: 提供代码覆盖率统计

代码地址

项目代码存储在以下仓库中:[项目链接](注:请勿直接填写外部链接,建议替换为具体的代码仓库路径)

转载地址:http://ythwz.baihongyu.com/

你可能感兴趣的文章
noi 1996 登山
查看>>
noi 7827 质数的和与积
查看>>
NOI-1.3-11-计算浮点数相除的余数
查看>>
NOI2010 海拔(平面图最大流)
查看>>
NOIp2005 过河
查看>>
NOIP2011T1 数字反转
查看>>
NOIP2014 提高组 Day2——寻找道路
查看>>
noip借教室 题解
查看>>
NOIP模拟测试19
查看>>
NOIp模拟赛二十九
查看>>
Vue3+element plus+sortablejs实现table列表拖拽
查看>>
Nokia5233手机和我装的几个symbian V5手机软件
查看>>
non linear processor
查看>>
Non-final field ‘code‘ in enum StateEnum‘
查看>>
none 和 host 网络的适用场景 - 每天5分钟玩转 Docker 容器技术(31)
查看>>
None还可以是函数定义可选参数的一个默认值,设置成默认值时实参在调用该函数时可以不输入与None绑定的元素...
查看>>
NoNodeAvailableException None of the configured nodes are available异常
查看>>
Vue.js 学习总结(16)—— 为什么 :deep、/deep/、>>> 样式能穿透到子组件
查看>>
nopcommerce商城系统--文档整理
查看>>
NOPI读取Excel
查看>>