spring-cloud-openfeign icon indicating copy to clipboard operation
spring-cloud-openfeign copied to clipboard

Add @FeignTest annotation for Feign Layer

Open yesmanmx opened this issue 3 years ago • 4 comments

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.

yesmanmx avatar Nov 12 '20 16:11 yesmanmx

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".

OlgaMaciaszek avatar Nov 12 '20 18:11 OlgaMaciaszek

Thank for reply

yesmanmx avatar Nov 12 '20 18:11 yesmanmx

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).

poznachowski avatar Nov 24 '21 09:11 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.

poznachowski avatar Dec 22 '21 11:12 poznachowski