I have a test for a Feign client and I would like to set up a test slice, like @WebMvcTest, @DataJpaTest, etc.
For example, the following test uses @SpringBootTest and it loads all the application context:
@SpringBootTest
@AutoConfigureWireMock(port = 0)
class AgePredictorFeignClientTest {
@Autowired
private AgePredictorFeignClient agePredictorFeignClient;
@Test
void getAge() {
stubFor(get(urlEqualTo("/age-api?name=Henrique"))
.willReturn(aResponse().withBodyFile("25_years_old.json")
.withHeader("Content-Type", "application/json")));
Integer age = agePredictorFeignClient.getAge("Henrique").getAge();
assertThat(age).isEqualTo(25);
verify(getRequestedFor(urlEqualTo("/age-api?name=Henrique")));
}
}
How could I change this test to load only the context related to Spring Cloud OpenFeign?
The source code for the application with this test is available at https://github.com/henriquels25/openfeign-tests-sample.
Currently there is no out of the box solution to have something like a @FeignTest. However, somebody made a solution for this to be found here. This solution Adds the desired functionality. It also has been proposed to spring-cloud-openfeign.
If you don't want to include the above depedency, I've created an example by manually importing some AutoConfiguration classes to only load necessary functionality. See here for a Feign client under test using this Test configuration
The only down-side of this, is that the wiremock random port can't expanded in time. That's why it has a fixed port.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With