diff --git a/docs/modules/ROOT/pages/spring-cloud-openfeign.adoc b/docs/modules/ROOT/pages/spring-cloud-openfeign.adoc index 28e323171..eb1664bc0 100644 --- a/docs/modules/ROOT/pages/spring-cloud-openfeign.adoc +++ b/docs/modules/ROOT/pages/spring-cloud-openfeign.adoc @@ -1074,6 +1074,61 @@ The URL provided in the configuration properties remains unused. |=== +[[testing-feign-clients]] +=== Testing Feign Clients + +The `spring-cloud-openfeign-test` module provides a `@FeignClientTest` annotation for tests that focus only on the Feign clients of an application, like the test slices of Spring Boot, such as `@RestClientTest`. +To use it, add the module to the test dependencies of your project, next to `spring-cloud-starter-openfeign`: + +[source,xml,indent=0] +---- + + org.springframework.cloud + spring-cloud-openfeign-test + test + +---- + +A `@FeignClientTest` test only enables the Feign, HTTP message converter and JSON auto-configuration. +Component scanning is limited to the configuration classes annotated with `@EnableFeignClients` and to the components that customize Feign clients, such as `RequestInterceptor`, `ErrorDecoder` or `FeignBuilderCustomizer` components. +The `@Bean` methods of other configuration classes are not loaded, so import the beans that a test needs with `@Import`. +The auto-configuration of circuit breakers, Micrometer and caching is not enabled either, so a fallback is not used unless the test enables it, for example with `@ImportAutoConfiguration`. +The Feign clients are the ones that the `@EnableFeignClients` annotation of the application registers, and a client is only created when it is injected, so the clients that a test does not use need no configuration. + +A test points a client at a stub server, such as WireMock, with the `spring.cloud.openfeign.client.config..url` property, as in the following example. +For a client with a `url` attribute, which takes precedence over that property, set the property that the attribute reads instead. + +[source,java,indent=0] +---- +@FeignClientTest +class GreetingClientTests { + + @RegisterExtension + static WireMockExtension wireMock = WireMockExtension.newInstance() + .options(wireMockConfig().dynamicPort()) + .build(); + + @DynamicPropertySource + static void greetingsUrl(DynamicPropertyRegistry registry) { + registry.add("spring.cloud.openfeign.client.config.greetings.url", wireMock::baseUrl); + } + + @Autowired + private GreetingClient greetingClient; + + @Test + void greet() { + wireMock.stubFor(get("/greetings/Ada").willReturn(okJson("{\"message\":\"Hello Ada\"}"))); + + assertThat(this.greetingClient.greet("Ada").message()).isEqualTo("Hello Ada"); + } + +} +---- + +To use the Feign clients in another test slice, such as `@WebMvcTest`, add the `@AutoConfigureFeign` annotation to the test, which enables the same auto-configuration. +It does not change the component scanning of that slice. + [[aot-and-native-image-support]] === AOT and Native Image Support diff --git a/pom.xml b/pom.xml index 9f63dc3c7..07b6ca62a 100644 --- a/pom.xml +++ b/pom.xml @@ -186,6 +186,7 @@ spring-cloud-openfeign-dependencies spring-cloud-openfeign-core + spring-cloud-openfeign-test spring-cloud-starter-openfeign docs diff --git a/spring-cloud-openfeign-dependencies/pom.xml b/spring-cloud-openfeign-dependencies/pom.xml index 15cf430c4..d1af43a9f 100644 --- a/spring-cloud-openfeign-dependencies/pom.xml +++ b/spring-cloud-openfeign-dependencies/pom.xml @@ -24,6 +24,11 @@ spring-cloud-openfeign-core ${project.version} + + org.springframework.cloud + spring-cloud-openfeign-test + ${project.version} + org.springframework.cloud spring-cloud-starter-openfeign diff --git a/spring-cloud-openfeign-test/pom.xml b/spring-cloud-openfeign-test/pom.xml new file mode 100644 index 000000000..20dfb7326 --- /dev/null +++ b/spring-cloud-openfeign-test/pom.xml @@ -0,0 +1,89 @@ + + + 4.0.0 + + org.springframework.cloud + spring-cloud-openfeign + 5.1.0-SNAPSHOT + .. + + + https://github.com/spring-cloud/spring-cloud-openfeign + + spring-cloud-openfeign-test + jar + Spring Cloud OpenFeign Test + Spring Cloud OpenFeign Test + + ${basedir}/.. + + + + org.springframework.cloud + spring-cloud-openfeign-core + + + org.springframework.boot + spring-boot-test-autoconfigure + + + org.springframework.boot + spring-boot-http-converter + + + io.github.openfeign + feign-core + + + org.springframework + spring-test + + + org.junit.jupiter + junit-jupiter-api + true + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-jackson + test + + + org.springframework.boot + spring-boot-webmvc-test + test + + + io.github.openfeign + feign-slf4j + test + + + jakarta.servlet + jakarta.servlet-api + test + + + org.springframework.cloud + spring-cloud-commons + test + + + org.springframework.cloud + spring-cloud-context + test + + + org.junit.platform + junit-platform-launcher + test + + + diff --git a/spring-cloud-openfeign-test/src/main/java/org/springframework/cloud/openfeign/test/autoconfigure/AutoConfigureFeign.java b/spring-cloud-openfeign-test/src/main/java/org/springframework/cloud/openfeign/test/autoconfigure/AutoConfigureFeign.java new file mode 100644 index 000000000..a912ba8a3 --- /dev/null +++ b/spring-cloud-openfeign-test/src/main/java/org/springframework/cloud/openfeign/test/autoconfigure/AutoConfigureFeign.java @@ -0,0 +1,46 @@ +/* + * Copyright 2013-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.openfeign.test.autoconfigure; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.test.autoconfigure.json.AutoConfigureJson; + +/** + * Annotation that can be applied to a test class to enable the auto-configuration of + * Feign clients, such as in another test slice than + * {@link FeignClientTest @FeignClientTest}. It does not change the component scanning of + * that slice. + * + * @author Sharang Gupta + * @since 5.1.0 + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@AutoConfigureJson +@ImportAutoConfiguration +public @interface AutoConfigureFeign { + +} diff --git a/spring-cloud-openfeign-test/src/main/java/org/springframework/cloud/openfeign/test/autoconfigure/FeignClientTest.java b/spring-cloud-openfeign-test/src/main/java/org/springframework/cloud/openfeign/test/autoconfigure/FeignClientTest.java new file mode 100644 index 000000000..9c0ec4372 --- /dev/null +++ b/spring-cloud-openfeign-test/src/main/java/org/springframework/cloud/openfeign/test/autoconfigure/FeignClientTest.java @@ -0,0 +1,119 @@ +/* + * Copyright 2013-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.openfeign.test.autoconfigure; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration; +import org.springframework.boot.test.context.filter.annotation.TypeExcludeFilters; +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.core.annotation.AliasFor; +import org.springframework.core.env.Environment; +import org.springframework.test.context.BootstrapWith; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +/** + * Annotation for a test that focuses only on the + * {@link FeignClient @FeignClient} interfaces of an application. + *

+ * Using this annotation only enables the auto-configuration that is relevant to Feign + * clients: the Feign, HTTP message converter and JSON auto-configuration. Similarly, + * component scanning is limited to the configuration classes annotated with + * {@link EnableFeignClients @EnableFeignClients} and to the components that customize + * Feign clients, such as {@code RequestInterceptor}, {@code ErrorDecoder} or + * {@code FeignBuilderCustomizer} components, as well as {@code JacksonModule} and + * {@code @JacksonComponent} beans. The {@code @Bean} methods of other configuration + * classes are not loaded, so the beans they declare need an + * {@link org.springframework.context.annotation.Import @Import}. The auto-configuration + * of circuit breakers, Micrometer and caching is not enabled either. + *

+ * The Feign clients are the ones that the {@code @EnableFeignClients} annotation of the + * application registers. A test points a client at a stub server, such as WireMock, with + * the {@code spring.cloud.openfeign.client.config..url} property, or with the + * property that the {@code url} attribute of its {@code @FeignClient} reads, since that + * attribute takes precedence. A client is only created when it is injected, so the + * clients that a test does not use need no configuration. + *

+ * For more fine-grained control, the {@link AutoConfigureFeign @AutoConfigureFeign} + * annotation adds the same auto-configuration to another test slice. + * + * @author Sharang Gupta + * @since 5.1.0 + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@BootstrapWith(FeignClientTestContextBootstrapper.class) +@ExtendWith(SpringExtension.class) +@OverrideAutoConfiguration(enabled = false) +@TypeExcludeFilters(FeignClientTypeExcludeFilter.class) +@AutoConfigureFeign +@ImportAutoConfiguration +public @interface FeignClientTest { + + /** + * Properties in form {@literal key=value} that should be added to the Spring + * {@link Environment} before the test runs. + * @return the properties to add + */ + String[] properties() default {}; + + /** + * Determines if default filtering should be used with + * {@link SpringBootApplication @SpringBootApplication}. By default, only the + * configuration classes annotated with {@code @EnableFeignClients} and the beans that + * customize Feign clients are included. + * @see #includeFilters() + * @see #excludeFilters() + * @return if default filters should be used + */ + boolean useDefaultFilters() default true; + + /** + * A set of include filters which can be used to add otherwise filtered beans to the + * application context. + * @return include filters to apply + */ + ComponentScan.Filter[] includeFilters() default {}; + + /** + * A set of exclude filters which can be used to filter beans that would otherwise be + * added to the application context. + * @return exclude filters to apply + */ + ComponentScan.Filter[] excludeFilters() default {}; + + /** + * Auto-configuration exclusions that should be applied for this test. + * @return auto-configuration exclusions to apply + */ + @AliasFor(annotation = ImportAutoConfiguration.class, attribute = "exclude") + Class[] excludeAutoConfiguration() default {}; + +} diff --git a/spring-cloud-openfeign-test/src/main/java/org/springframework/cloud/openfeign/test/autoconfigure/FeignClientTestContextBootstrapper.java b/spring-cloud-openfeign-test/src/main/java/org/springframework/cloud/openfeign/test/autoconfigure/FeignClientTestContextBootstrapper.java new file mode 100644 index 000000000..3ad145503 --- /dev/null +++ b/spring-cloud-openfeign-test/src/main/java/org/springframework/cloud/openfeign/test/autoconfigure/FeignClientTestContextBootstrapper.java @@ -0,0 +1,29 @@ +/* + * Copyright 2013-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.openfeign.test.autoconfigure; + +import org.springframework.boot.test.autoconfigure.TestSliceTestContextBootstrapper; +import org.springframework.test.context.TestContextBootstrapper; + +/** + * {@link TestContextBootstrapper} for {@link FeignClientTest @FeignClientTest} support. + * + * @author Sharang Gupta + */ +class FeignClientTestContextBootstrapper extends TestSliceTestContextBootstrapper { + +} diff --git a/spring-cloud-openfeign-test/src/main/java/org/springframework/cloud/openfeign/test/autoconfigure/FeignClientTypeExcludeFilter.java b/spring-cloud-openfeign-test/src/main/java/org/springframework/cloud/openfeign/test/autoconfigure/FeignClientTypeExcludeFilter.java new file mode 100644 index 000000000..aa978db7f --- /dev/null +++ b/spring-cloud-openfeign-test/src/main/java/org/springframework/cloud/openfeign/test/autoconfigure/FeignClientTypeExcludeFilter.java @@ -0,0 +1,105 @@ +/* + * Copyright 2013-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.openfeign.test.autoconfigure; + +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.Set; + +import feign.Capability; +import feign.Client; +import feign.Contract; +import feign.Logger; +import feign.QueryMapEncoder; +import feign.RequestInterceptor; +import feign.ResponseInterceptor; +import feign.Retryer; +import feign.codec.Decoder; +import feign.codec.Encoder; +import feign.codec.ErrorDecoder; + +import org.springframework.boot.context.TypeExcludeFilter; +import org.springframework.boot.http.converter.autoconfigure.ClientHttpMessageConvertersCustomizer; +import org.springframework.boot.test.context.filter.annotation.StandardAnnotationCustomizableTypeExcludeFilter; +import org.springframework.cloud.openfeign.AnnotatedParameterProcessor; +import org.springframework.cloud.openfeign.CircuitBreakerNameResolver; +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.cloud.openfeign.FallbackFactory; +import org.springframework.cloud.openfeign.FeignBuilderCustomizer; +import org.springframework.cloud.openfeign.FeignFormatterRegistrar; +import org.springframework.cloud.openfeign.FeignLoggerFactory; +import org.springframework.cloud.openfeign.clientconfig.FeignClientConfigurer; +import org.springframework.cloud.openfeign.support.HttpMessageConverterCustomizer; +import org.springframework.util.ClassUtils; + +/** + * {@link TypeExcludeFilter} for {@link FeignClientTest @FeignClientTest}. + * + * @author Sharang Gupta + */ +class FeignClientTypeExcludeFilter extends StandardAnnotationCustomizableTypeExcludeFilter { + + private static final String[] OPTIONAL_INCLUDES = { "tools.jackson.databind.JacksonModule", + "org.springframework.boot.jackson.JacksonComponent", "com.fasterxml.jackson.databind.Module", + "org.springframework.boot.jackson2.JsonComponent" }; + + private static final Set> KNOWN_INCLUDES; + + static { + Set> includes = new LinkedHashSet<>(); + includes.add(EnableFeignClients.class); + includes.add(RequestInterceptor.class); + includes.add(ResponseInterceptor.class); + includes.add(Encoder.class); + includes.add(Decoder.class); + includes.add(ErrorDecoder.class); + includes.add(Contract.class); + includes.add(Retryer.class); + includes.add(Logger.class); + includes.add(Capability.class); + includes.add(QueryMapEncoder.class); + includes.add(Client.class); + includes.add(FeignBuilderCustomizer.class); + includes.add(FeignFormatterRegistrar.class); + includes.add(FeignLoggerFactory.class); + includes.add(AnnotatedParameterProcessor.class); + includes.add(FeignClientConfigurer.class); + includes.add(FallbackFactory.class); + includes.add(CircuitBreakerNameResolver.class); + includes.add(HttpMessageConverterCustomizer.class); + includes.add(ClientHttpMessageConvertersCustomizer.class); + for (String optionalInclude : OPTIONAL_INCLUDES) { + try { + includes.add(ClassUtils.forName(optionalInclude, null)); + } + catch (Exception ex) { + // Ignore + } + } + KNOWN_INCLUDES = Collections.unmodifiableSet(includes); + } + + FeignClientTypeExcludeFilter(Class testClass) { + super(testClass); + } + + @Override + protected Set> getKnownIncludes() { + return KNOWN_INCLUDES; + } + +} diff --git a/spring-cloud-openfeign-test/src/main/resources/META-INF/spring/org.springframework.cloud.openfeign.test.autoconfigure.AutoConfigureFeign.imports b/spring-cloud-openfeign-test/src/main/resources/META-INF/spring/org.springframework.cloud.openfeign.test.autoconfigure.AutoConfigureFeign.imports new file mode 100644 index 000000000..9e693fbf1 --- /dev/null +++ b/spring-cloud-openfeign-test/src/main/resources/META-INF/spring/org.springframework.cloud.openfeign.test.autoconfigure.AutoConfigureFeign.imports @@ -0,0 +1,6 @@ +org.springframework.boot.http.converter.autoconfigure.HttpMessageConvertersAutoConfiguration +org.springframework.cloud.openfeign.FeignAutoConfiguration +org.springframework.cloud.openfeign.encoding.FeignAcceptGzipEncodingAutoConfiguration +org.springframework.cloud.openfeign.encoding.FeignContentGzipEncodingAutoConfiguration +org.springframework.cloud.openfeign.hateoas.FeignHalAutoConfiguration +org.springframework.cloud.openfeign.loadbalancer.FeignLoadBalancerAutoConfiguration diff --git a/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/FeignClientTypeExcludeFilterTests.java b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/FeignClientTypeExcludeFilterTests.java new file mode 100644 index 000000000..b39d9ac69 --- /dev/null +++ b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/FeignClientTypeExcludeFilterTests.java @@ -0,0 +1,164 @@ +/* + * Copyright 2013-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.openfeign.test.autoconfigure; + +import java.io.IOException; +import java.lang.reflect.Method; +import java.util.List; + +import feign.RequestInterceptor; +import feign.RequestTemplate; +import feign.Response; +import feign.Target; +import feign.codec.ErrorDecoder; +import org.junit.jupiter.api.Test; + +import org.springframework.boot.http.converter.autoconfigure.ClientHttpMessageConvertersCustomizer; +import org.springframework.cloud.openfeign.CircuitBreakerNameResolver; +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.cloud.openfeign.FallbackFactory; +import org.springframework.cloud.openfeign.clientconfig.FeignClientConfigurer; +import org.springframework.cloud.openfeign.support.HttpMessageConverterCustomizer; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.type.classreading.MetadataReader; +import org.springframework.core.type.classreading.MetadataReaderFactory; +import org.springframework.core.type.classreading.SimpleMetadataReaderFactory; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.HttpMessageConverters; +import org.springframework.stereotype.Service; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link FeignClientTypeExcludeFilter}. + * + * @author Sharang Gupta + */ +class FeignClientTypeExcludeFilterTests { + + private final MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); + + @Test + void doesNotExcludeTheComponentsThatCustomizeFeignClients() throws IOException { + FeignClientTypeExcludeFilter filter = new FeignClientTypeExcludeFilter(WithDefaultFilters.class); + + assertThat(excludes(filter, ExampleFeignClientsConfiguration.class)).isFalse(); + assertThat(excludes(filter, ExampleRequestInterceptor.class)).isFalse(); + assertThat(excludes(filter, ExampleErrorDecoder.class)).isFalse(); + assertThat(excludes(filter, ExampleFallbackFactory.class)).isFalse(); + assertThat(excludes(filter, ExampleCircuitBreakerNameResolver.class)).isFalse(); + assertThat(excludes(filter, ExampleFeignClientConfigurer.class)).isFalse(); + assertThat(excludes(filter, ExampleHttpMessageConverterCustomizer.class)).isFalse(); + assertThat(excludes(filter, ExampleClientHttpMessageConvertersCustomizer.class)).isFalse(); + } + + @Test + void excludesOtherComponents() throws IOException { + FeignClientTypeExcludeFilter filter = new FeignClientTypeExcludeFilter(WithDefaultFilters.class); + + assertThat(excludes(filter, ExampleService.class)).isTrue(); + } + + @Test + void excludesTheComponentsThatCustomizeFeignClientsWithoutDefaultFilters() throws IOException { + FeignClientTypeExcludeFilter filter = new FeignClientTypeExcludeFilter(WithoutDefaultFilters.class); + + assertThat(excludes(filter, ExampleRequestInterceptor.class)).isTrue(); + } + + private boolean excludes(FeignClientTypeExcludeFilter filter, Class type) throws IOException { + MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(type.getName()); + return filter.match(metadataReader, this.metadataReaderFactory); + } + + @FeignClientTest + static class WithDefaultFilters { + + } + + @FeignClientTest(useDefaultFilters = false) + static class WithoutDefaultFilters { + + } + + @Configuration(proxyBeanMethods = false) + @EnableFeignClients + static class ExampleFeignClientsConfiguration { + + } + + static class ExampleRequestInterceptor implements RequestInterceptor { + + @Override + public void apply(RequestTemplate template) { + } + + } + + static class ExampleErrorDecoder implements ErrorDecoder { + + @Override + public Exception decode(String methodKey, Response response) { + return new IllegalStateException(methodKey); + } + + } + + static class ExampleFallbackFactory implements FallbackFactory { + + @Override + public Object create(Throwable cause) { + return new Object(); + } + + } + + static class ExampleCircuitBreakerNameResolver implements CircuitBreakerNameResolver { + + @Override + public String resolveCircuitBreakerName(String feignClientName, Target target, Method method) { + return feignClientName; + } + + } + + static class ExampleFeignClientConfigurer implements FeignClientConfigurer { + + } + + static class ExampleHttpMessageConverterCustomizer implements HttpMessageConverterCustomizer { + + @Override + public void accept(List> converters) { + } + + } + + static class ExampleClientHttpMessageConvertersCustomizer implements ClientHttpMessageConvertersCustomizer { + + @Override + public void customize(HttpMessageConverters.ClientBuilder builder) { + } + + } + + @Service + static class ExampleService { + + } + +} diff --git a/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/StubServer.java b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/StubServer.java new file mode 100644 index 000000000..6cc9adbdb --- /dev/null +++ b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/StubServer.java @@ -0,0 +1,86 @@ +/* + * Copyright 2013-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.openfeign.test.autoconfigure; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.UncheckedIOException; +import java.net.InetSocketAddress; +import java.nio.charset.StandardCharsets; + +import com.sun.net.httpserver.Headers; +import com.sun.net.httpserver.HttpServer; + +/** + * An HTTP server for the tests that answers every request with the same JSON body and + * records the last request. + * + * @author Sharang Gupta + */ +public final class StubServer implements AutoCloseable { + + private final HttpServer server; + + private volatile RecordedRequest lastRequest; + + private StubServer(String json) { + try { + this.server = HttpServer.create(new InetSocketAddress("localhost", 0), 0); + } + catch (IOException ex) { + throw new UncheckedIOException(ex); + } + byte[] body = json.getBytes(StandardCharsets.UTF_8); + this.server.createContext("/", exchange -> { + this.lastRequest = new RecordedRequest(exchange.getRequestURI().getPath(), exchange.getRequestHeaders()); + exchange.getResponseHeaders().set("Content-Type", "application/json"); + exchange.sendResponseHeaders(200, body.length); + try (OutputStream responseBody = exchange.getResponseBody()) { + responseBody.write(body); + } + }); + this.server.start(); + } + + public static StubServer respondingWith(String json) { + return new StubServer(json); + } + + public String url() { + return "http://localhost:" + this.server.getAddress().getPort(); + } + + public RecordedRequest lastRequest() { + return this.lastRequest; + } + + @Override + public void close() { + this.server.stop(0); + } + + /** + * The path and the headers of a request that the server received. + * + * @param path the path of the request + * @param headers the headers of the request + */ + public record RecordedRequest(String path, Headers headers) { + + } + +} diff --git a/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/AutoConfigureFeignTests.java b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/AutoConfigureFeignTests.java new file mode 100644 index 000000000..a1e36c3ae --- /dev/null +++ b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/AutoConfigureFeignTests.java @@ -0,0 +1,62 @@ +/* + * Copyright 2013-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.openfeign.test.autoconfigure.application; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest; +import org.springframework.cloud.openfeign.test.autoconfigure.AutoConfigureFeign; +import org.springframework.cloud.openfeign.test.autoconfigure.StubServer; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.springframework.test.web.servlet.assertj.MockMvcTester; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link AutoConfigureFeign @AutoConfigureFeign} in a + * {@link WebMvcTest @WebMvcTest}. + * + * @author Sharang Gupta + */ +@WebMvcTest(GreetingController.class) +@AutoConfigureFeign +class AutoConfigureFeignTests { + + private static final StubServer server = StubServer.respondingWith("{\"message\":\"Hello Ada\"}"); + + @Autowired + private MockMvcTester mvc; + + @DynamicPropertySource + static void greetingsUrl(DynamicPropertyRegistry registry) { + registry.add("spring.cloud.openfeign.client.config.greetings.url", server::url); + } + + @AfterAll + static void stopServer() { + server.close(); + } + + @Test + void controllerUsesTheFeignClient() { + assertThat(this.mvc.get().uri("/hello/Ada")).hasStatusOk().hasBodyTextEqualTo("Hello Ada"); + } + +} diff --git a/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/ExampleApplication.java b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/ExampleApplication.java new file mode 100644 index 000000000..1715f1e98 --- /dev/null +++ b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/ExampleApplication.java @@ -0,0 +1,31 @@ +/* + * Copyright 2013-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.openfeign.test.autoconfigure.application; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.openfeign.EnableFeignClients; + +/** + * An application that enables its Feign clients on its main class. + * + * @author Sharang Gupta + */ +@SpringBootApplication +@EnableFeignClients +public class ExampleApplication { + +} diff --git a/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/FeignClientTestPropertiesTests.java b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/FeignClientTestPropertiesTests.java new file mode 100644 index 000000000..0fcc1b1c7 --- /dev/null +++ b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/FeignClientTestPropertiesTests.java @@ -0,0 +1,45 @@ +/* + * Copyright 2013-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.openfeign.test.autoconfigure.application; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.openfeign.test.autoconfigure.FeignClientTest; +import org.springframework.core.env.Environment; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for the {@link FeignClientTest#properties() properties} of + * {@link FeignClientTest @FeignClientTest}. + * + * @author Sharang Gupta + */ +@FeignClientTest(properties = "spring.cloud.openfeign.client.config.greetings.url=http://localhost:8080") +class FeignClientTestPropertiesTests { + + @Autowired + private Environment environment; + + @Test + void propertiesAreAddedToTheEnvironment() { + assertThat(this.environment.getProperty("spring.cloud.openfeign.client.config.greetings.url")) + .isEqualTo("http://localhost:8080"); + } + +} diff --git a/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/FeignClientTestTests.java b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/FeignClientTestTests.java new file mode 100644 index 000000000..17274186a --- /dev/null +++ b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/FeignClientTestTests.java @@ -0,0 +1,76 @@ +/* + * Copyright 2013-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.openfeign.test.autoconfigure.application; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.openfeign.test.autoconfigure.FeignClientTest; +import org.springframework.cloud.openfeign.test.autoconfigure.StubServer; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link FeignClientTest @FeignClientTest} with an application that enables its + * Feign clients on its main class. + * + * @author Sharang Gupta + */ +@FeignClientTest +class FeignClientTestTests { + + private static final StubServer server = StubServer.respondingWith("{\"message\":\"Hello Ada\"}"); + + @Autowired + private GreetingClient greetingClient; + + @Autowired + private ApplicationContext applicationContext; + + @DynamicPropertySource + static void greetingsUrl(DynamicPropertyRegistry registry) { + registry.add("spring.cloud.openfeign.client.config.greetings.url", server::url); + } + + @AfterAll + static void stopServer() { + server.close(); + } + + @Test + void feignClientDecodesTheResponseOfTheServer() { + assertThat(this.greetingClient.greet("Ada")).isEqualTo(new Greeting("Hello Ada")); + assertThat(server.lastRequest().path()).isEqualTo("/greetings/Ada"); + } + + @Test + void requestInterceptorComponentIsApplied() { + this.greetingClient.greet("Ada"); + + assertThat(server.lastRequest().headers().getFirst("X-Trace-Id")).isEqualTo("test-trace"); + } + + @Test + void otherComponentsAreNotLoaded() { + assertThat(this.applicationContext.getBeanProvider(GreetingController.class).getIfAvailable()).isNull(); + } + +} diff --git a/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/Greeting.java b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/Greeting.java new file mode 100644 index 000000000..e540ad5f4 --- /dev/null +++ b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/Greeting.java @@ -0,0 +1,27 @@ +/* + * Copyright 2013-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.openfeign.test.autoconfigure.application; + +/** + * A greeting of the greeting service. + * + * @param message the message of the greeting + * @author Sharang Gupta + */ +public record Greeting(String message) { + +} diff --git a/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/GreetingClient.java b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/GreetingClient.java new file mode 100644 index 000000000..17a93835d --- /dev/null +++ b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/GreetingClient.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.openfeign.test.autoconfigure.application; + +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; + +/** + * A Feign client of a greeting service. + * + * @author Sharang Gupta + */ +@FeignClient("greetings") +public interface GreetingClient { + + @GetMapping("/greetings/{name}") + Greeting greet(@PathVariable("name") String name); + +} diff --git a/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/GreetingController.java b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/GreetingController.java new file mode 100644 index 000000000..1bb3e1737 --- /dev/null +++ b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/GreetingController.java @@ -0,0 +1,42 @@ +/* + * Copyright 2013-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.openfeign.test.autoconfigure.application; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +/** + * A controller that uses the {@link GreetingClient}. + * + * @author Sharang Gupta + */ +@RestController +class GreetingController { + + private final GreetingClient greetingClient; + + GreetingController(GreetingClient greetingClient) { + this.greetingClient = greetingClient; + } + + @GetMapping("/hello/{name}") + String hello(@PathVariable("name") String name) { + return this.greetingClient.greet(name).message(); + } + +} diff --git a/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/InventoryClient.java b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/InventoryClient.java new file mode 100644 index 000000000..40f017354 --- /dev/null +++ b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/InventoryClient.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.openfeign.test.autoconfigure.application; + +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; + +/** + * A Feign client that no test injects and that has no URL, to check that it is not + * created. + * + * @author Sharang Gupta + */ +@FeignClient("inventory") +public interface InventoryClient { + + @GetMapping("/stock") + String stock(); + +} diff --git a/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/TraceIdRequestInterceptor.java b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/TraceIdRequestInterceptor.java new file mode 100644 index 000000000..2bbe19e4b --- /dev/null +++ b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/application/TraceIdRequestInterceptor.java @@ -0,0 +1,37 @@ +/* + * Copyright 2013-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.openfeign.test.autoconfigure.application; + +import feign.RequestInterceptor; +import feign.RequestTemplate; + +import org.springframework.stereotype.Component; + +/** + * A {@link RequestInterceptor} component that adds a trace id to every request. + * + * @author Sharang Gupta + */ +@Component +class TraceIdRequestInterceptor implements RequestInterceptor { + + @Override + public void apply(RequestTemplate template) { + template.header("X-Trace-Id", "test-trace"); + } + +} diff --git a/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/configuration/ExampleApplication.java b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/configuration/ExampleApplication.java new file mode 100644 index 000000000..ba7d43191 --- /dev/null +++ b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/configuration/ExampleApplication.java @@ -0,0 +1,29 @@ +/* + * Copyright 2013-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.openfeign.test.autoconfigure.configuration; + +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * An application that enables its Feign clients in a configuration class. + * + * @author Sharang Gupta + */ +@SpringBootApplication +public class ExampleApplication { + +} diff --git a/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/configuration/ExampleFeignConfiguration.java b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/configuration/ExampleFeignConfiguration.java new file mode 100644 index 000000000..57fa7ee88 --- /dev/null +++ b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/configuration/ExampleFeignConfiguration.java @@ -0,0 +1,32 @@ +/* + * Copyright 2013-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.openfeign.test.autoconfigure.configuration; + +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.cloud.openfeign.test.autoconfigure.application.GreetingClient; +import org.springframework.context.annotation.Configuration; + +/** + * A configuration class that enables the Feign clients of the application. + * + * @author Sharang Gupta + */ +@Configuration(proxyBeanMethods = false) +@EnableFeignClients(clients = GreetingClient.class) +class ExampleFeignConfiguration { + +} diff --git a/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/configuration/FeignClientTestWithFeignClientsConfigurationTests.java b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/configuration/FeignClientTestWithFeignClientsConfigurationTests.java new file mode 100644 index 000000000..7b4be93c8 --- /dev/null +++ b/spring-cloud-openfeign-test/src/test/java/org/springframework/cloud/openfeign/test/autoconfigure/configuration/FeignClientTestWithFeignClientsConfigurationTests.java @@ -0,0 +1,61 @@ +/* + * Copyright 2013-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.openfeign.test.autoconfigure.configuration; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.openfeign.test.autoconfigure.FeignClientTest; +import org.springframework.cloud.openfeign.test.autoconfigure.StubServer; +import org.springframework.cloud.openfeign.test.autoconfigure.application.Greeting; +import org.springframework.cloud.openfeign.test.autoconfigure.application.GreetingClient; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link FeignClientTest @FeignClientTest} with an application that enables its + * Feign clients in a configuration class. + * + * @author Sharang Gupta + */ +@FeignClientTest +class FeignClientTestWithFeignClientsConfigurationTests { + + private static final StubServer server = StubServer.respondingWith("{\"message\":\"Hello Grace\"}"); + + @Autowired + private GreetingClient greetingClient; + + @DynamicPropertySource + static void greetingsUrl(DynamicPropertyRegistry registry) { + registry.add("spring.cloud.openfeign.client.config.greetings.url", server::url); + } + + @AfterAll + static void stopServer() { + server.close(); + } + + @Test + void feignClientsOfTheConfigurationClassAreRegistered() { + assertThat(this.greetingClient.greet("Grace")).isEqualTo(new Greeting("Hello Grace")); + } + +}