gpt4 book ai didi

spring-boot - Spring Boot 测试尝试第二次初始化 cache2k 并失败

转载 作者:行者123 更新时间:2023-12-04 12:14:22 26 4
gpt4 key购买 nike

将 cache2k 添加到我的项目后一些 @SpringBootTest因错误而停止工作:

java.lang.IllegalStateException: Cache already created: 'cache'



下面我提供了重现的最小示例:

前往 start.spring.io 并使用 Cache starter 创建一个最简单的 Maven 项目,然后添加 cache2k 依赖项:
<properties>
<java.version>1.8</java.version>
<cache2k-version>1.2.2.Final</cache2k-version>
</properties>

<dependencies>
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-api</artifactId>
<version>${cache2k-version}</version>
</dependency>
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-core</artifactId>
<version>${cache2k-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-spring</artifactId>
<version>${cache2k-version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

现在配置最简单的缓存:
@SpringBootApplication
@EnableCaching
public class CachingDemoApplication {

public static void main(String[] args) {
SpringApplication.run(CachingDemoApplication.class, args);
}

@Bean
public CacheManager springCacheManager() {
SpringCache2kCacheManager cacheManager = new SpringCache2kCacheManager();
cacheManager.addCaches(b -> b.name("cache"));
return cacheManager;
}

}

并添加任何服务(我们将 @MockBean 在我们的一项测试中:
@Service
public class SomeService {
public String getString() {
System.out.println("Executing service method");
return "foo";
}
}

现在两个 @SpringBootTest需要测试来重现问题:
@SpringBootTest
@RunWith(SpringRunner.class)
public class SpringBootAppTest {

@Test
public void getString() {
System.out.println("Empty test");
}
}

@RunWith(SpringRunner.class)
@SpringBootTest
public class WithMockedBeanTest {

@MockBean
SomeService service;

@Test
public void contextLoads() {
}
}

请注意,第二个测试模拟了 @MockBean .这会导致错误(下面的堆栈跟踪)。
Caused by: java.lang.IllegalStateException: Cache already created: 'cache'
at org.cache2k.core.CacheManagerImpl.newCache(CacheManagerImpl.java:174)
at org.cache2k.core.InternalCache2kBuilder.buildAsIs(InternalCache2kBuilder.java:239)
at org.cache2k.core.InternalCache2kBuilder.build(InternalCache2kBuilder.java:182)
at org.cache2k.core.Cache2kCoreProviderImpl.createCache(Cache2kCoreProviderImpl.java:215)
at org.cache2k.Cache2kBuilder.build(Cache2kBuilder.java:837)
at org.cache2k.extra.spring.SpringCache2kCacheManager.buildAndWrap(SpringCache2kCacheManager.java:205)
at org.cache2k.extra.spring.SpringCache2kCacheManager.lambda$addCache$2(SpringCache2kCacheManager.java:143)
at java.util.concurrent.ConcurrentHashMap.compute(ConcurrentHashMap.java:1853)
at org.cache2k.extra.spring.SpringCache2kCacheManager.addCache(SpringCache2kCacheManager.java:141)
at org.cache2k.extra.spring.SpringCache2kCacheManager.addCaches(SpringCache2kCacheManager.java:132)
at com.example.cachingdemo.CachingDemoApplication.springCacheManager(CachingDemoApplication.java:23)
at com.example.cachingdemo.CachingDemoApplication$$EnhancerBySpringCGLIB$$2dce99ca.CGLIB$springCacheManager$0(<generated>)
at com.example.cachingdemo.CachingDemoApplication$$EnhancerBySpringCGLIB$$2dce99ca$$FastClassBySpringCGLIB$$bbd240c0.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)
at com.example.cachingdemo.CachingDemoApplication$$EnhancerBySpringCGLIB$$2dce99ca.springCacheManager(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 52 more

如果删除 @MockBean ,两个测试都会通过。

如何在我的测试套件中避免此错误?

最佳答案

您的第二个测试代表了不同的 ApplicationContext总之,测试框架将为它启动一个专用的。如 cache2k是有状态的(例如为给定的类加载器共享 CacheManager,如果它已经存在),第二个上下文将尝试创建一个新的 CacheManager而第一个仍然处于事件状态。

您要么需要将其中一个测试标记为脏(请参阅 @DirtiesContext ),这将关闭上下文并关闭 CacheManager ,或者您可以用不需要所有这些的选项替换缓存基础结构,请参阅 @AutoConfigureCache .

cache2k以这样一种方式工作,它需要您弄脏上下文,我强烈建议使用后面的选项来交换它。

关于spring-boot - Spring Boot 测试尝试第二次初始化 cache2k 并失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57201103/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com