Skip to content

Add @FeignTest annotation for Feign Layer #427

Open

Description

@yesmanmx

Hello!
I started to integrate Spring Cloud Contract into existing project.

No problems on producer side. Base test class annotated with @WebMvcTest, so only Web Layer getting configured and has same state as for production deployment. Services mocked, Web works, tests passed, stubs published

Issue comes on consumer side.
We are using Feign as client. So Preferable solution is to create test, inject FeignClient with same configuration, as for production deployment and perform http requests using using Feign abstraction.

Example from documentation:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.NONE)
@AutoConfigureStubRunner(ids = {"com.example:http-server-dsl:+:stubs:6565"},
        stubsMode = StubRunnerProperties.StubsMode.LOCAL)
public class LoanApplicationServiceTests

This will work, but in services with complex configuration it is difficult to create such configuration (@SpringBootTest).
All properties must be set, production only beans must be mocked, etc. More over - there is no need for Feign tests to create all service, configuration and controller beans.

So existent of special annotation for Feign Client Layer Test (@FeignTest) will helps a lot and sounds reasonable.
But to create this annotation deep knowledge of all Feign related spring auto configurations required (web converters, ribbon, eureka, jackson, etc.)

It would be great, if you will able to implement this annotation.

Thanks in advance,
Yesman Maxim.

Activity

  1. OlgaMaciaszek commented on Nov 12, 2020

    @OlgaMaciaszek
    Collaborator

    Hello, thanks for proposing this. @SpringBootTest provides the possibility to pass the exact configuration to use in the test and is very flexible. The feature you describe is a possible enhancement, but we must first verify how much interest in such a feature there is in the community. Marking as "waiting for votes".

  2. yesmanmx commented on Nov 12, 2020

    @yesmanmx
    Author

    Thank for reply

  3. poznachowski commented on Nov 24, 2021

    @poznachowski

    Hey @OlgaMaciaszek. I came across this issue and wanted to share something I did recently at work (and used with success):
    https://github.com/poznachowski/feigntest
    This is what we would call a Feign slice test: Only things related to Feign (and Wiremock) are bootstrapped. It follows the same code structure as other Spring Boot slice tests. However, it is tailored a bit for my company's need: we always use @FeignClient url parameter, which is then substituted by the url of Wiremock.

    If you find this (or something with this shape) worth adding - I'd be glad to contribute (preceded with some guidance).

  4. poznachowski commented on Dec 22, 2021

    @poznachowski

    @OlgaMaciaszek trying to draw a little more attention :) I can try and submit a MR request already, but still got some concerns about how to do it right.
    My solution, for instance, is based on additional dependency of spring-cloud-contract-wiremock. I'd assume it should rather be an opt-in solution, that can be recommended in the docs.

  5. sharanggupta commented on Oct 15, 2026

    @sharanggupta
    Contributor

    Hi @ryanjbaxter, this is the most voted open issue in the project, so I'd like to contribute it if you're open to it. Here is a sketch, modelled on Spring Boot's @RestClientTest:

    • A @FeignClientTest slice, with an optional clients attribute. It bootstraps the listed @FeignClient interfaces (or all of them when none are listed), the Feign auto-configuration and the HTTP message converters, and leaves out the other components of the application.
    • It would not pick a stub server. Tests point a client at WireMock, MockWebServer or a Spring Cloud Contract stub with the usual spring.cloud.openfeign.client.config.<name>.url property, and the docs would show a WireMock example.
    • An @AutoConfigureFeign annotation that adds the same auto-configuration to another slice or to a @SpringBootTest.

    It could ship as a small spring-cloud-openfeign-test module, or as an optional test support package in spring-cloud-openfeign-core if you'd rather not add a module. I'm happy to adjust the shape before starting.

  6. ryanjbaxter commented on Oct 15, 2026

    @ryanjbaxter
    Contributor

    Spring Cloud Contract is no longer part of Spring Cloud. It was moved to stubborn.sh, so I am not sure this is really useful any longer

  7. sharanggupta commented on Oct 15, 2026

    @sharanggupta
    Contributor

    Thanks @ryanjbaxter. The slice would not depend on Spring Cloud Contract. It would only bootstrap the @FeignClient interfaces and the Feign auto-configuration, and the test would point the client at any stub server through spring.cloud.openfeign.client.config.<name>.url. That could be WireMock or MockWebServer, the same way @RestClientTest works with MockRestServiceServer. I only mentioned Contract stubs as one of the options.

    If you still do not think it is worth adding, I am happy to leave it.

  8. ryanjbaxter commented on Oct 15, 2026

    @ryanjbaxter
    Contributor

    But why can't you just point the feign client at that mock server using configuration?

  9. sharanggupta commented on Oct 15, 2026

    @sharanggupta
    Contributor

    You can, and that works today with a @SpringBootTest. The slice would add only what @RestClientTest adds over a @SpringBootTest with MockRestServiceServer: the test context starts with just the Feign clients, the Feign auto-configuration and the message converters. So a client test does not need the database, broker or security setup of the rest of the application.

    Without it, a test narrows the context by hand with @ImportAutoConfiguration, and the list to import changes between Boot versions. In Boot 4, for example, the message converter and Jackson auto-configurations moved to new modules. The slice would keep that list in one place.

    That is the whole benefit, so if you do not think it is worth a test module, I am fine with leaving it.

  10. ryanjbaxter commented on Oct 15, 2026

    @ryanjbaxter
    Contributor

    I guess we can see what the PR would look like for this. But IMO it would be something we would hold off on merging until the next major comes around.

  11. sharanggupta commented on Oct 15, 2026

    @sharanggupta
    Contributor

    Thanks @ryanjbaxter, that makes sense. I will put the PR together so you can see the shape, and holding it for the next major is fine.

  12. sharanggupta commented on Oct 15, 2026

    @sharanggupta
    Contributor

    I opened #1449 with the slice, so you can see what it would look like. It is a separate spring-cloud-openfeign-test module with @FeignClientTest and @AutoConfigureFeign, and the description explains the design choices. As you said, it can wait for the next major.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementwaiting for votes

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions