|
| 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.test.autoconfigure.web.reactive; |
| 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.autoconfigure.json.AutoConfigureJson; |
| 32 | +import org.springframework.boot.test.context.SpringBootTest; |
| 33 | +import org.springframework.boot.test.context.filter.annotation.TypeExcludeFilters; |
| 34 | +import org.springframework.boot.webflux.test.autoconfigure.AutoConfigureWebFlux; |
| 35 | +import org.springframework.boot.webtestclient.autoconfigure.AutoConfigureWebTestClient; |
| 36 | +import org.springframework.context.annotation.ComponentScan; |
| 37 | +import org.springframework.context.annotation.Import; |
| 38 | +import org.springframework.core.annotation.AliasFor; |
| 39 | +import org.springframework.core.env.Environment; |
| 40 | +import org.springframework.test.context.BootstrapWith; |
| 41 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 42 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 43 | + |
| 44 | +/** |
| 45 | + * Annotation that can be used for a Spring WebFlux test that focuses |
| 46 | + * <strong>only</strong> on Spring WebFlux components. |
| 47 | + * <p> |
| 48 | + * Using this annotation only enables auto-configuration that is relevant to WebFlux |
| 49 | + * tests. Similarly, component scanning is limited to beans annotated with: |
| 50 | + * <ul> |
| 51 | + * <li>{@code @Controller}</li> |
| 52 | + * <li>{@code @ControllerAdvice}</li> |
| 53 | + * <li>{@code @JacksonComponent}</li> |
| 54 | + * <li>{@code @JsonComponent} (Jackson 2, deprecated)</li> |
| 55 | + * </ul> |
| 56 | + * <p> |
| 57 | + * as well as beans that implement: |
| 58 | + * <ul> |
| 59 | + * <li>{@code Converter}</li> |
| 60 | + * <li>{@code GenericConverter}</li> |
| 61 | + * <li>{@code IDialect}, if Thymeleaf is available</li> |
| 62 | + * <li>{@code JacksonModule}, if Jackson is available</li> |
| 63 | + * <li>{@code Module}, if Jackson 2 is available (deprecated)</li> |
| 64 | + * <li>{@code WebExceptionHandler}</li> |
| 65 | + * <li>{@code WebFluxConfigurer}</li> |
| 66 | + * <li>{@code WebFilter}</li> |
| 67 | + * </ul> |
| 68 | + * <p> |
| 69 | + * By default, tests annotated with {@code @WebFluxTest} will also auto-configure a |
| 70 | + * {@link WebTestClient}. For more fine-grained control of WebTestClient the |
| 71 | + * {@link AutoConfigureWebTestClient @AutoConfigureWebTestClient} annotation can be used. |
| 72 | + * <p> |
| 73 | + * Typically {@code @WebFluxTest} is used in combination with |
| 74 | + * {@link org.springframework.test.context.bean.override.mockito.MockitoBean @MockitoBean} |
| 75 | + * or {@link Import @Import} to create any collaborators required by your |
| 76 | + * {@code @Controller} beans. |
| 77 | + * <p> |
| 78 | + * If you are looking to load your full application configuration and use WebTestClient, |
| 79 | + * you should consider {@link SpringBootTest @SpringBootTest} combined with |
| 80 | + * {@link AutoConfigureWebTestClient @AutoConfigureWebTestClient} rather than this |
| 81 | + * annotation. |
| 82 | + * <p> |
| 83 | + * When using JUnit 4, this annotation should be used in combination with |
| 84 | + * {@code @RunWith(SpringRunner.class)}. |
| 85 | + * |
| 86 | + * @author Stephane Nicoll |
| 87 | + * @author Artsiom Yudovin |
| 88 | + * @since 4.0.0 |
| 89 | + * @see AutoConfigureWebFlux |
| 90 | + * @see AutoConfigureWebTestClient |
| 91 | + */ |
| 92 | +@Target(ElementType.TYPE) |
| 93 | +@Retention(RetentionPolicy.RUNTIME) |
| 94 | +@Documented |
| 95 | +@Inherited |
| 96 | +@BootstrapWith(WebFluxTestContextBootstrapper.class) |
| 97 | +@ExtendWith(SpringExtension.class) |
| 98 | +@OverrideAutoConfiguration(enabled = false) |
| 99 | +@TypeExcludeFilters(WebFluxTypeExcludeFilter.class) |
| 100 | +@AutoConfigureJson |
| 101 | +@AutoConfigureWebFlux |
| 102 | +@AutoConfigureWebTestClient |
| 103 | +@ImportAutoConfiguration |
| 104 | +public @interface WebFluxTest { |
| 105 | + |
| 106 | + /** |
| 107 | + * Properties in form {@literal key=value} that should be added to the Spring |
| 108 | + * {@link Environment} before the test runs. |
| 109 | + * @return the properties to add |
| 110 | + */ |
| 111 | + String[] properties() default {}; |
| 112 | + |
| 113 | + /** |
| 114 | + * Specifies the controllers to test. This is an alias of {@link #controllers()} which |
| 115 | + * can be used for brevity if no other attributes are defined. See |
| 116 | + * {@link #controllers()} for details. |
| 117 | + * @see #controllers() |
| 118 | + * @return the controllers to test |
| 119 | + */ |
| 120 | + @AliasFor("controllers") |
| 121 | + Class<?>[] value() default {}; |
| 122 | + |
| 123 | + /** |
| 124 | + * Specifies the controllers to test. May be left blank if all {@code @Controller} |
| 125 | + * beans should be added to the application context. |
| 126 | + * @see #value() |
| 127 | + * @return the controllers to test |
| 128 | + */ |
| 129 | + @AliasFor("value") |
| 130 | + Class<?>[] controllers() default {}; |
| 131 | + |
| 132 | + /** |
| 133 | + * Determines if default filtering should be used with |
| 134 | + * {@link SpringBootApplication @SpringBootApplication}. By default only |
| 135 | + * {@code @Controller} (when no explicit {@link #controllers() controllers} are |
| 136 | + * defined), {@code @ControllerAdvice} and {@code WebFluxConfigurer} beans are |
| 137 | + * included. |
| 138 | + * @see #includeFilters() |
| 139 | + * @see #excludeFilters() |
| 140 | + * @return if default filters should be used |
| 141 | + */ |
| 142 | + boolean useDefaultFilters() default true; |
| 143 | + |
| 144 | + /** |
| 145 | + * A set of include filters which can be used to add otherwise filtered beans to the |
| 146 | + * application context. |
| 147 | + * @return include filters to apply |
| 148 | + */ |
| 149 | + ComponentScan.Filter[] includeFilters() default {}; |
| 150 | + |
| 151 | + /** |
| 152 | + * A set of exclude filters which can be used to filter beans that would otherwise be |
| 153 | + * added to the application context. |
| 154 | + * @return exclude filters to apply |
| 155 | + */ |
| 156 | + ComponentScan.Filter[] excludeFilters() default {}; |
| 157 | + |
| 158 | + /** |
| 159 | + * Auto-configuration exclusions that should be applied for this test. |
| 160 | + * @return auto-configuration exclusions to apply |
| 161 | + */ |
| 162 | + @AliasFor(annotation = ImportAutoConfiguration.class, attribute = "exclude") |
| 163 | + Class<?>[] excludeAutoConfiguration() default {}; |
| 164 | + |
| 165 | +} |
0 commit comments