spring-cloud / spring-cloud-netflix

Integration with Netflix OSS components
http://cloud.spring.io/spring-cloud-netflix/
Apache License 2.0
4.87k stars 2.44k forks source link

Mocking URLs for FeignClients with Eureka disabled in integration tests #4162

Closed harluss closed 1 year ago

harluss commented 1 year ago

Hi, I have a FeignClient I use with Eureka Service:

@FeignClient(name = "${feign.clients.hangar.name}", path = "${feign.clients.hangar.path}")
public interface SpaceshipClient {
  // api calls...
}

Works wonders live but I can't find any information on how to mock/assign an URL for it for Integration Tests with Eureka Service disabled (enabled: false)? When I setup WireMock and try to run the test I get this error:

FeignClientFactoryBean : For 'hangar' URL not provided. Will try picking an instance via load-balancing.

If I add url = "${feign.clients.hangar.url}" to the Feign annotation and set matching value in test profile in application.yml tests work, but when Eureka is enabled in "prod" the app crashes on startup.

It seems like something simple, yet I can't find any information on how to do it. How to provide URL for each client with Eureka disabled without adding the url property in the annotation? Can I either provide a URL (or a list of URLs if I use multiple clients calling different microservices) to either Eureka or Load Balancer in application.yml? Or how to overwrite the url property in application.yml with values from enabled Eureka, something like `${eureka.whatever.url}?

I guess I could manually create 2 beans for each client and activate one in prod and other one in tests, that I would prefer to have one implementation and just change config values based on profile. Is that even possible?

I use spring-cloud-starter-openfeign, spring-cloud-starter-netflix-eureka-client and spring-cloud-contract-wiremock (mocks) with spring-cloud-dependencies version 2021.0.5.

I would greatly appreciate any help with this.

spencergibb commented 1 year ago

Maybe use simple discovery client (always enabled) and disable eureka for tests. https://docs.spring.io/spring-cloud-commons/docs/current/reference/html/#simplediscoveryclient

harluss commented 1 year ago

Thank you @spencergibb, that was exactly what I was looking for. Using SimpleDiscoveryClient with Eureka disabled for tests allowed me to stick to one implementation of FeignClient.

Test profile in application.yml for the FeignClient above:

spring:
  config:
    activate:
      on-profile: test
  cloud:
    discovery:
      client:
        simple:
          instances:
            hangar[0]:
              uri: http://localhost:${wiremock.server.port:0}

eureka:
  client:
    enabled: false