前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >设置Spring单元测试的外部依赖

设置Spring单元测试的外部依赖

作者头像
十毛
发布2019-12-02 21:25:27
1.2K0
发布2019-12-02 21:25:27
举报

单元测试中,有时候也依赖外部的组件,比如Redis-Mock。Spring Boot Test需要在上下文启动之前,先启动Redis-Mock,否则上下文会启动失败

依赖的外部组件

比如单元测试中依赖redis-mock,就必须在Spring上下文下载之前就先启动Redis Server,否则就会报下面的错误

代码语言:javascript
复制
io.netty.channel.AbstractChannel$AnnotatedConnectException: 
Connection refused: no further information: localhost/127.0.0.1:6379

方法一:在每个Specification中配置RedisServer

代码语言:javascript
复制
@SpringBootTest
@ActiveProfiles("unittest")
@Transactional
class OrderManagerTest extends Specification {
    private static RedisServer redisServer = null

    def "setupSpec"() {
        try {
            redisServer = RedisServer.newRedisServer()
            redisServer.start()
            System.setProperty("spring.redis.port", Integer.toString(redisServer.getBindPort()))
        } catch (IOException e) {
            throw new RuntimeException(e)
        }
    }

    def "cleanupSpec"() {
        if (redisServer != null) {
            redisServer.stop()
        }
    }
}

更好的方法

上面方法,需要在每个Specification都配置Redis Server,存在大量的冗余。其实可以利用BeanFactoryPostProcessorApplicationListener接口实现

代码语言:javascript
复制
import com.github.tenmao.redismock.RedisServer;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationContextEvent;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class UnittestCustomContext implements BeanFactoryPostProcessor, ApplicationListener<ApplicationContextEvent> {
    private static RedisServer redisServer = null;

    @Override
    public void onApplicationEvent(ApplicationContextEvent event) {
        if (event instanceof ContextClosedEvent) {
            //Spring上下文结束后清理自定义上下文
            System.out.println("tenmao unit test for event: " + event);
            if (redisServer != null) {
                redisServer.stop();
            }
        }
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        try {
            //Spring上下文初始化之前 配置自定义上下文,也可以修改Spring配置信息
            redisServer = RedisServer.newRedisServer();
            redisServer.start();
            System.setProperty("spring.redis.port", Integer.toString(redisServer.getBindPort()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 依赖的外部组件
  • 方法一:在每个Specification中配置RedisServer
  • 更好的方法
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档