001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.test.autoconfigure.web.reactive;
018
019import java.lang.annotation.Documented;
020import java.lang.annotation.ElementType;
021import java.lang.annotation.Inherited;
022import java.lang.annotation.Retention;
023import java.lang.annotation.RetentionPolicy;
024import java.lang.annotation.Target;
025
026import org.junit.jupiter.api.extension.ExtendWith;
027
028import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
029import org.springframework.boot.autoconfigure.SpringBootApplication;
030import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
031import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
032import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
033import org.springframework.boot.test.autoconfigure.json.AutoConfigureJson;
034import org.springframework.boot.test.context.SpringBootTest;
035import org.springframework.boot.test.mock.mockito.MockBean;
036import org.springframework.context.annotation.ComponentScan;
037import org.springframework.context.annotation.Import;
038import org.springframework.core.annotation.AliasFor;
039import org.springframework.core.env.Environment;
040import org.springframework.test.context.BootstrapWith;
041import org.springframework.test.context.junit.jupiter.SpringExtension;
042import org.springframework.test.web.reactive.server.WebTestClient;
043
044/**
045 * Annotation that can be used in combination with {@code @RunWith(SpringRunner.class)}
046 * for a typical Spring WebFlux test. Can be used when a test focuses
047 * <strong>only</strong> on Spring WebFlux components.
048 * <p>
049 * Using this annotation will disable full auto-configuration and instead apply only
050 * configuration relevant to WebFlux tests (i.e. {@code @Controller},
051 * {@code @ControllerAdvice}, {@code @JsonComponent},
052 * {@code Converter}/{@code GenericConverter}, and {@code WebFluxConfigurer} beans but not
053 * {@code @Component}, {@code @Service} or {@code @Repository} beans).
054 * <p>
055 * By default, tests annotated with {@code @WebFluxTest} will also auto-configure a
056 * {@link WebTestClient}. For more fine-grained control of WebTestClient the
057 * {@link AutoConfigureWebTestClient @AutoConfigureWebTestClient} annotation can be used.
058 * <p>
059 * Typically {@code @WebFluxTest} is used in combination with {@link MockBean @MockBean}
060 * or {@link Import @Import} to create any collaborators required by your
061 * {@code @Controller} beans.
062 * <p>
063 * If you are looking to load your full application configuration and use WebTestClient,
064 * you should consider {@link SpringBootTest @SpringBootTest} combined with
065 * {@link AutoConfigureWebTestClient @AutoConfigureWebTestClient} rather than this
066 * annotation.
067 *
068 * @author Stephane Nicoll
069 * @author Artsiom Yudovin
070 * @since 2.0.0
071 * @see AutoConfigureWebFlux
072 * @see AutoConfigureWebTestClient
073 */
074@Target(ElementType.TYPE)
075@Retention(RetentionPolicy.RUNTIME)
076@Documented
077@Inherited
078@BootstrapWith(WebFluxTestContextBootstrapper.class)
079@ExtendWith(SpringExtension.class)
080@OverrideAutoConfiguration(enabled = false)
081@TypeExcludeFilters(WebFluxTypeExcludeFilter.class)
082@AutoConfigureCache
083@AutoConfigureJson
084@AutoConfigureWebFlux
085@AutoConfigureWebTestClient
086@ImportAutoConfiguration
087public @interface WebFluxTest {
088
089        /**
090         * Properties in form {@literal key=value} that should be added to the Spring
091         * {@link Environment} before the test runs.
092         * @return the properties to add
093         * @since 2.1.0
094         */
095        String[] properties() default {};
096
097        /**
098         * Specifies the controllers to test. This is an alias of {@link #controllers()} which
099         * can be used for brevity if no other attributes are defined. See
100         * {@link #controllers()} for details.
101         * @see #controllers()
102         * @return the controllers to test
103         */
104        @AliasFor("controllers")
105        Class<?>[] value() default {};
106
107        /**
108         * Specifies the controllers to test. May be left blank if all {@code @Controller}
109         * beans should be added to the application context.
110         * @see #value()
111         * @return the controllers to test
112         */
113        @AliasFor("value")
114        Class<?>[] controllers() default {};
115
116        /**
117         * Determines if default filtering should be used with
118         * {@link SpringBootApplication @SpringBootApplication}. By default only
119         * {@code @Controller} (when no explicit {@link #controllers() controllers} are
120         * defined), {@code @ControllerAdvice} and {@code WebFluxConfigurer} beans are
121         * included.
122         * @see #includeFilters()
123         * @see #excludeFilters()
124         * @return if default filters should be used
125         */
126        boolean useDefaultFilters() default true;
127
128        /**
129         * A set of include filters which can be used to add otherwise filtered beans to the
130         * application context.
131         * @return include filters to apply
132         */
133        ComponentScan.Filter[] includeFilters() default {};
134
135        /**
136         * A set of exclude filters which can be used to filter beans that would otherwise be
137         * added to the application context.
138         * @return exclude filters to apply
139         */
140        ComponentScan.Filter[] excludeFilters() default {};
141
142        /**
143         * Auto-configuration exclusions that should be applied for this test.
144         * @return auto-configuration exclusions to apply
145         */
146        @AliasFor(annotation = ImportAutoConfiguration.class, attribute = "exclude")
147        Class<?>[] excludeAutoConfiguration() default {};
148
149}