@testconfiguration用于嵌入式redis,相同的redis服务器用于所有测试类

wecizke3  于 2021-06-08  发布在  Redis
关注(0)|答案(1)|浏览(380)

我使用嵌入式redis进行集成测试。我根据以下文章使用redis设置了测试:https://www.baeldung.com/spring-embedded-redis.
问题是,如果我想将@testconfiguration与几个测试类一起使用,就会出现错误,因为没有调用@predestroy。嵌入的redis每次都是新创建的,不在测试类之间共享。因此,每次尝试在同一端口上创建新的redis sserver时,都会导致以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testRedisConfiguration': 
Invocation of init method failed; nested exception is java.lang.RuntimeException: 
Can't start redis server. Check logs for details.

当我将@dirtiescontext添加到所有测试类时,会出现以下错误:

java.lang.IllegalStateException: org.springframework.web.context.support.GenericWebApplicationContext@255e5e2e has been closed already

有没有一种方法可以对我所有的测试类使用相同的嵌入式redis?

shstlldc

shstlldc1#

您可以使用 @BeforeClass 注解。这将限制整个测试类的redis缓存构造一次。

相关问题