spring-cloud / spring-cloud-openfeign

Support for using OpenFeign in Spring Cloud apps
Apache License 2.0
1.22k stars 786 forks source link

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

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? Or how to overwrite the url property in application.yml with values from enabled Eureka, something like `${eureka.whatever.url}?

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 guess I could manually create 2 beans or 2 different interfaces extending base one 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?

Update: By Integration Tests I don't mean just testing the client itself but the whole app - send request to the app, app does it's logic and uses Feign client to hit other microservices (this is the part I'm trying to address here).

I would greatly appreciate any help with this.

harluss commented 1 year ago

Solution I was looking for was to use SimpleDiscoveryClient, which allowed me to stick with one implementation of the FeignClient mentioned 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