更新
下面列出的我的类(class) (ServiceDiscoveryConfiguration
) 从未被使用过。即使我删除 @EnableDiscoveryClient
为了尝试完全避免设置,它仍然尝试连接到 Consul。
唯一对我有用的是完全删除 Consul Maven 依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
如果不通过配置文件和注释设置,我该怎么做才能阻止 Consul 运行单元测试?
原来的
我有一个使用 Spring Consul 的应用程序。
我设置了一个类来启用这样的发现:
@Profile ("!" + Profiles.UNIT_TEST)
@Configuration
@EnableDiscoveryClient
public class ServiceDiscoveryConfiguration {
}
这个应该 如果我没记错的话,将禁用领事部分。基本测试类(它是一个
abstract
在我的所有单元测试之间共享)是使用以下注释设置的。这就是我认为问题所在。@SpringBootTest (classes = Service.class)
@WebAppConfiguration
@TestExecutionListeners (...)
@DirtiesContext
@ActiveProfiles (Profiles.UNIT_TEST)
@Test (...)
public abstract class AbstractBootTest extends AbstractTestNGSpringContextTests {
// ...
}
当我执行测试时,我得到:
Caused by: com.ecwid.consul.transport.TransportException: java.net.ConnectException: Connection refused
这让我相信配置文件激活不起作用或我使用
!
的语法@Profile
上的运算符(operator)规范没有做我认为应该做的事情。根执行类本身具有基本注释,包括 @ComponentScan
我知道的注释已经扫描了适当的包。帮助?
最佳答案
您可以通过禁用
@TestPropertySource(properties = {"spring.cloud.consul.config.enabled=false"})
关于Spring Consul - 禁用单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39622363/