gpt4 book ai didi

java - 注释@ConditionalOnMissingBean 在测试中不匹配

转载 作者:行者123 更新时间:2023-11-29 07:25:29 26 4
gpt4 key购买 nike

我有一个接口(interface)的两个实现 - default 和 dev。我将 @ConditionalOnProperty 用于默认实现,将 @Profile@ConditionalOnMissingBean 组合用于开发实现。

@Service
@ConditionalOnProperty(prefix = "keystore", value = "file")
public class DefaultKeyStoreService implements KeyStoreService {

@Service
@Profile("dev")
@ConditionalOnMissingBean(KeyStoreService.class)
public class DevKeyStoreService implements KeyStoreService {

现在,问题出在 DevKeyStoreService 的测试 DevKeyStoreServiceTest 中。我有这样的配置:

@SpringBootTest(
classes = {DevKeyStoreService.class},
properties = {"keystore.file="}
)
@RunWith(SpringRunner.class)
@ActiveProfiles("dev")
public class DevKeyStoreServiceTest {

@Autowired
private DevKeyStoreService tested;

@Test
public void testPrivateKey() {
} //... etc.

结果是:

Negative matches:
-----------------
DevKeyStoreService:
Did not match:
- @ConditionalOnMissingBean (types: service.crypto.KeyStoreService; SearchStrategy: all) found bean 'devKeyStoreService' (OnBeanCondition)

和典型的 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'service.crypto.DevKeyStoreServiceTest'...

如何配置测试类才能运行它?

最佳答案

好的,我明白了。

具有值 @ConditionalOnMissingBean(KeyStoreService.class) 的注释试图仅查找具体实例,KeyStoreService 接口(interface)不是。这样,它找不到任何东西。

当我改为使用带有类型的注解时,它就像一个魅力:@ConditionalOnMissingBean(type = "KeyStoreService")

关于java - 注释@ConditionalOnMissingBean 在测试中不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53883327/

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