spring boot 测试的时候不注册某些服务

今天在写spring boot 单元测试的时候,往redis的stream写入数据,消息立马就没有了,测试类如下

 

@SpringBootTest(classes = {DemoApplication.class}, properties = {"spring.profiles.active=unittest"})
class DemoApplicationTests {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private RedisService redisService;


    @Test
    public void testAdd() throws InterruptedException {
        final int max_thread = 1;
        CountDownLatch countDownLatch = new CountDownLatch(max_thread);
        long begin = System.currentTimeMillis();
        for (int i = 0; i < max_thread; i++) {
            redisService.xaddBatch(countDownLatch);
        }
        countDownLatch.await();
        long end = System.currentTimeMillis();
        logger.info("总共写入数据{}条数据,总耗时{}s", RedisService.TOTAL_COUNT, (end - begin) / 1000);
    }

}

原来是代码里头有一个服务,一启动就自动读消息导致的

 

@Service
public class CustomStreamListenerProcessor {

    @Autowired
    private Executor executor;
    @Autowired(required = false)
    private List<CustomStreamListener> listeners;
    @Autowired
    private StringRedisTemplate template;

    @PostConstruct
    public void doReadMsg() {
        if (listeners != null && listeners.size() > 0) {
            listeners.stream().forEach(item -> executor.execute(new CustomStreamListenerWrapper(item, template)));
        }
    }


}

@PostConstruct注解表示这个类初始化完成执行这个方法,在这个方法里头会自动消费消息,导致我始终无法读到消息

怎么办呢,最简单的办法是,把CustomStreamListenerProcessor 的@Service的注解去掉,然后测试完再加回去,这显然不是好办法。

其中一个办法使用Profile,就是是往CustomStreamListenerProcessor 的@Service加一个@Profile("!unittest"),表示只有在非unittest情况下才能注册使用。然后再测试类中修改下面的代码,加上

@SpringBootTest(classes = {DemoApplication.class}, properties = {"spring.profiles.active=unittest"})

表示启用unittest的profile

 

这就搞定了,不要改来改去代码,一些服务类只在非测试profiles才注册使用

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值