|
| 1 | +/* |
| 2 | + * Copyright 2012-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.webclient.test.autoconfigure; |
| 18 | + |
| 19 | +import java.lang.annotation.Documented; |
| 20 | +import java.lang.annotation.ElementType; |
| 21 | +import java.lang.annotation.Inherited; |
| 22 | +import java.lang.annotation.Retention; |
| 23 | +import java.lang.annotation.RetentionPolicy; |
| 24 | +import java.lang.annotation.Target; |
| 25 | + |
| 26 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 27 | + |
| 28 | +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; |
| 29 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 30 | +import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration; |
| 31 | +import org.springframework.boot.test.context.filter.annotation.TypeExcludeFilters; |
| 32 | +import org.springframework.context.annotation.ComponentScan; |
| 33 | +import org.springframework.core.annotation.AliasFor; |
| 34 | +import org.springframework.core.env.Environment; |
| 35 | +import org.springframework.test.context.BootstrapWith; |
| 36 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 37 | +import org.springframework.web.client.RestClient.Builder; |
| 38 | + |
| 39 | +/** |
| 40 | + * Annotation for a Spring WebClient test that focuses <strong>only</strong> on beans that |
| 41 | + * use {@link Builder WebClient.Builder}. |
| 42 | + * <p> |
| 43 | + * Using this annotation only enables auto-configuration that is relevant to rest client |
| 44 | + * tests. Similarly, component scanning is limited to beans annotated with: |
| 45 | + * <ul> |
| 46 | + * <li>{@code @JsonComponent}</li> |
| 47 | + * </ul> |
| 48 | + * <p> |
| 49 | + * as well as beans that implement: |
| 50 | + * <ul> |
| 51 | + * <li>{@code JacksonModule}, if Jackson is available</li> |
| 52 | + * </ul> |
| 53 | + * <p> |
| 54 | + * When using JUnit 4, this annotation should be used in combination with |
| 55 | + * {@code @RunWith(SpringRunner.class)}. |
| 56 | + * |
| 57 | + * @author Andy Wilkinson |
| 58 | + * @since 4.0.0 |
| 59 | + */ |
| 60 | +@Target(ElementType.TYPE) |
| 61 | +@Retention(RetentionPolicy.RUNTIME) |
| 62 | +@Documented |
| 63 | +@Inherited |
| 64 | +@BootstrapWith(WebClientTestContextBootstrapper.class) |
| 65 | +@ExtendWith(SpringExtension.class) |
| 66 | +@OverrideAutoConfiguration(enabled = false) |
| 67 | +@TypeExcludeFilters(WebClientTypeExcludeFilter.class) |
| 68 | +@AutoConfigureWebClient |
| 69 | +@ImportAutoConfiguration |
| 70 | +public @interface WebClientTest { |
| 71 | + |
| 72 | + /** |
| 73 | + * Properties in form {@literal key=value} that should be added to the Spring |
| 74 | + * {@link Environment} before the test runs. |
| 75 | + * @return the properties to add |
| 76 | + */ |
| 77 | + String[] properties() default {}; |
| 78 | + |
| 79 | + /** |
| 80 | + * Specifies the components to test. This is an alias of {@link #components()} which |
| 81 | + * can be used for brevity if no other attributes are defined. See |
| 82 | + * {@link #components()} for details. |
| 83 | + * @see #components() |
| 84 | + * @return the components to test |
| 85 | + */ |
| 86 | + @AliasFor("components") |
| 87 | + Class<?>[] value() default {}; |
| 88 | + |
| 89 | + /** |
| 90 | + * Specifies the components to test. May be left blank if components will be manually |
| 91 | + * imported or created directly. |
| 92 | + * @see #value() |
| 93 | + * @return the components to test |
| 94 | + */ |
| 95 | + @AliasFor("value") |
| 96 | + Class<?>[] components() default {}; |
| 97 | + |
| 98 | + /** |
| 99 | + * Determines if default filtering should be used with |
| 100 | + * {@link SpringBootApplication @SpringBootApplication}. By default only |
| 101 | + * {@code @JsonComponent} and {@code Module} beans are included. |
| 102 | + * @see #includeFilters() |
| 103 | + * @see #excludeFilters() |
| 104 | + * @return if default filters should be used |
| 105 | + */ |
| 106 | + boolean useDefaultFilters() default true; |
| 107 | + |
| 108 | + /** |
| 109 | + * A set of include filters which can be used to add otherwise filtered beans to the |
| 110 | + * application context. |
| 111 | + * @return include filters to apply |
| 112 | + */ |
| 113 | + ComponentScan.Filter[] includeFilters() default {}; |
| 114 | + |
| 115 | + /** |
| 116 | + * A set of exclude filters which can be used to filter beans that would otherwise be |
| 117 | + * added to the application context. |
| 118 | + * @return exclude filters to apply |
| 119 | + */ |
| 120 | + ComponentScan.Filter[] excludeFilters() default {}; |
| 121 | + |
| 122 | + /** |
| 123 | + * Auto-configuration exclusions that should be applied for this test. |
| 124 | + * @return auto-configuration exclusions to apply |
| 125 | + */ |
| 126 | + @AliasFor(annotation = ImportAutoConfiguration.class, attribute = "exclude") |
| 127 | + Class<?>[] excludeAutoConfiguration() default {}; |
| 128 | + |
| 129 | +} |
0 commit comments