Reputation: 1863
My class listed below (ServiceDiscoveryConfiguration
) is never being utilized. Even if I remove the @EnableDiscoveryClient
to attempt to completely avoid the setup, it still attempts to connect to Consul.
The only thing that worked for me was removing the Consul Maven depdency completely:
<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>
What can I do to prevent Consul for running for unit tests if not through the profile and annotation setup?
I have an application using Spring Consul. I have a class set up to enable discovery like this:
@Profile ("!" + Profiles.UNIT_TEST)
@Configuration
@EnableDiscoveryClient
public class ServiceDiscoveryConfiguration {
}
This should be disabling the Consul portion, if I am not mistaken. The base test class (it's an abstract
shared between all of my unit tests) is setup up with the following annotations. This is where I think the problem is.
@SpringBootTest (classes = Service.class)
@WebAppConfiguration
@TestExecutionListeners (...)
@DirtiesContext
@ActiveProfiles (Profiles.UNIT_TEST)
@Test (...)
public abstract class AbstractBootTest extends AbstractTestNGSpringContextTests {
// ...
}
When I execute my tests I get:
Caused by: com.ecwid.consul.transport.TransportException: java.net.ConnectException: Connection refused
This leads me to believe that the profile activation is not working or my syntax using the !
operator on the @Profile
specification is not doing what I thought it was supposed to be doing. The root execution class itself has basic annotations including a @ComponentScan
annotation that I know has the appropriate packages being scanned.
Assistance?
Upvotes: 3
Views: 4353
Reputation: 953
if you have bootstrap.properties
file in your project, you should create bootstrap.properties
file under test/resources
:
spring.application.name=<service-name>
spring.cloud.bus.enabled=false
spring.cloud.discovery.enabled=false
spring.cloud.consul.enabled=false
spring.cloud.consul.config.enabled=false
This will disable consul integration in tests
Upvotes: 4
Reputation: 2911
Add below property in your application.properties file under test/resources
spring.cloud.discovery.enabled=false
spring.cloud.consul.enabled=false
spring.cloud.consul.config.enabled=false
This will disable consul integration while testing your application
Upvotes: 3
Reputation: 1871
You can disable via
@TestPropertySource(properties = {"spring.cloud.consul.config.enabled=false"})
Upvotes: 5
Reputation: 3065
The problem is that if you have spring-consul in the classpath it will try to auto-configure it anyway
Upvotes: 1
Reputation: 9536
The profile annotation is correct
@Profile ("!" + Profiles.UNIT_TEST)
the profile activation looks ok, also
@ActiveProfiles (Profiles.UNIT_TEST)
You wrote you are using ComponentScan, that is important, because @Profile annotation on a bean is ignored, if the bean is instantiated by a @Bean annotated method. May be you check again, to see, this does not happen ?
To narrow down the problem you can try :
set a breakpoint in the constructor of ServiceDiscoveryConfiguration, to see if it is instantiated or not
remove @EnableDiscoveryClient to see if this is really the cause of your problems
Upvotes: 0