Disabling Spring cloud consul when running Test
Asked Answered
C

4

8

I like to run my unit test with test spring profile without consul. I'm trying to use spring.cloud.config.enabled:false and disabling EnableDiscoveryClient annotation for test profile but it doesn't work.

I'm using spring.cloud.consul 1.0.0.BUILD-SNAPSHOT. Here is the exception:

com.ecwid.consul.transport.TransportException: java.net.ConnectException: Connection refused: connect at com.ecwid.consul.transport.DefaultHttpTransport.executeRequest(DefaultHttpTransport.java:87) at com.ecwid.consul.transport.DefaultHttpTransport.makeGetRequest(DefaultHttpTransport.java:46) at com.ecwid.consul.v1.ConsulRawClient.makeGetRequest(ConsulRawClient.java:66)

Calm answered 29/2, 2016 at 11:20 Comment(2)
"doesn't work" isn't real helpful. What error do you get? What version are you using?Martins
Where did you set spring.cloud.config.enabled:false and how did you disable EnableDiscoveryClient? The full exception would be helpful to see if it came from discovery or config.Martins
C
18

Try disabling Consul with spring.cloud.consul.enabled:false in your profile.

I use @IntegrationTest({"spring.cloud.consul.enabled=false"}) tag to disable consul for my tests, as a class tag. I found that on the Spring Cloud Consul GitHub issue tracker, here.

The other suggestion from that post was to use something like the following as your bootstrap.yml.

spring:
  application:
    name: <myServiceName>
  cloud:
    bus:
      enabled: false
    discovery:
      enabled: false
    consul:
      enabled: false
      config:
        enabled: false
Clardy answered 25/4, 2016 at 14:17 Comment(0)
N
0

AFAIK, once you put spring-cloud-starter-consul-config, you can't really disable it when you try to import it. So my workaround is just import the consul regardless and then use "optional" so it won't throw error.

# will import regardless, but it's okay as "optional" won't throw error
spring.config.import: "optional:consul:"


spring.cloud.consul:
  host: ${CONSUL_HOST:localhost}
  port: ${CONSUL_PORT:8500}
  config:
    enabled: ${CONSUL_ENABLE:true}
    prefix: ${CONSUL_CONFIG:config}
    fail-fast: true
    data-key: application.yaml
    name: ${spring.application.name}
Naphthol answered 20/2 at 4:15 Comment(0)
P
-1

Try @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)

Prehistory answered 9/9, 2019 at 4:8 Comment(0)
T
-1

Add it to

@SpringBootTest(properties = {"spring.cloud.consul.enabled=false", "spring.cloud.consul.discovery.enabled=false"})

or test/resources/bootstrap.yml as

spring.cloud.consul.enabled: false spring.cloud.consul.discovery.enabled: false

Topographer answered 31/8, 2020 at 10:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.