Skip/Mock Redis In Junit

2019-04-16 00:39发布

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

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

2条回答
Ridiculous、
2楼-- · 2019-04-16 00:58

Annotate with @Ignore to ignore the method. Like this:

@Ignore("reason of skipping")
@Test
public void testConnectionCreation(){
    // do some stuff...
}

Optionally you can provide a note to why the test is ignored as shown above.

See http://junit.sourceforge.net/javadoc/org/junit/Ignore.html for more info.

查看更多
看我几分像从前
3楼-- · 2019-04-16 01:16

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.

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.

查看更多
登录 后发表回答