本文介绍了在Junit中跳过/模拟Redis的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试一个服务,该服务又会与Redis建立连接.

I want to test a service which in turns create connection with redis.

我想在我的junit中跳过这一部分.有没有办法跳过此方法调用或对其进行模拟?

I want to skip this part in my junit. Is there a way to skip this method call or mock it?

推荐答案

我认为问题更多是关于如何模拟Redis部分,以便在Redis不可用时进行测试.这很困难,因为您的服务可能正在使用连接,因此您必须进行很多模拟.在Spring Boot中,我们要做的就是检查Redis服务器在localhost上是否可用,如果是这种情况,请运行测试,否则跳过测试.

I think the question was more about how the Redis part can be mocked so that the test run when redis isn't available. It's hard because your service is probably using the connection so you'd have to do a lot of mocking. What we do in Spring Boot is check if a redis server is available on localhost and if that's the case run the tests, otherwise skip.

请参见 RedisTestServer 示例用法.请注意,该规则适用于所有测试,因此您可能希望将使用Redis的测试移到隔离的测试类中.

See RedisTestServer and a sample usage. Note that the rule applies to all the tests so you may want to move the tests that are using Redis in an isolated test class.

这篇关于在Junit中跳过/模拟Redis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 04:22