博客
关于我
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/

你可能感兴趣的文章
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>
MYSQL:基础——3N范式的表结构设计
查看>>
MYSQL:基础——触发器
查看>>
Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 参数--lock-tables浅析
查看>>
mysqldump 导出中文乱码
查看>>
mysqldump 导出数据库中每张表的前n条
查看>>
mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
查看>>