Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions docs/modules/ROOT/pages/spring-cloud-openfeign.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
----
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-test</artifactId>
<scope>test</scope>
</dependency>
----

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

Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
<modules>
<module>spring-cloud-openfeign-dependencies</module>
<module>spring-cloud-openfeign-core</module>
<module>spring-cloud-openfeign-test</module>
<module>spring-cloud-starter-openfeign</module>
<module>docs</module>
</modules>
Expand Down
5 changes: 5 additions & 0 deletions spring-cloud-openfeign-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<artifactId>spring-cloud-openfeign-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-test</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
Expand Down
89 changes: 89 additions & 0 deletions spring-cloud-openfeign-test/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign</artifactId>
<version>5.1.0-SNAPSHOT</version>
<relativePath>..</relativePath> <!-- lookup parent from repository -->
</parent>
<scm>
<url>https://github.com/spring-cloud/spring-cloud-openfeign</url>
</scm>
<artifactId>spring-cloud-openfeign-test</artifactId>
<packaging>jar</packaging>
<name>Spring Cloud OpenFeign Test</name>
<description>Spring Cloud OpenFeign Test</description>
<properties>
<main.basedir>${basedir}/..</main.basedir>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-http-converter</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-jackson</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-webmvc-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-slf4j</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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 {

}
Original file line number Diff line number Diff line change
@@ -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 <strong>only</strong> on the
* {@link FeignClient @FeignClient} interfaces of an application.
* <p>
* 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.
* <p>
* 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.<name>.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.
* <p>
* 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 {};

}
Original file line number Diff line number Diff line change
@@ -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<FeignClientTest> {

}
Loading