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.client; 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.web.client.RestTemplateBuilder; 034import org.springframework.context.annotation.ComponentScan; 035import org.springframework.core.annotation.AliasFor; 036import org.springframework.core.env.Environment; 037import org.springframework.stereotype.Component; 038import org.springframework.test.context.BootstrapWith; 039import org.springframework.test.context.junit.jupiter.SpringExtension; 040import org.springframework.test.web.client.MockRestServiceServer; 041import org.springframework.web.client.RestTemplate; 042 043/** 044 * Annotation that can be used in combination with {@code @RunWith(SpringRunner.class)} 045 * for a typical Spring rest client test. Can be used when a test focuses 046 * <strong>only</strong> on beans that use {@link RestTemplateBuilder}. 047 * <p> 048 * Using this annotation will disable full auto-configuration and instead apply only 049 * configuration relevant to rest client tests (i.e. Jackson or GSON auto-configuration 050 * and {@code @JsonComponent} beans, but not regular {@link Component @Component} beans). 051 * <p> 052 * By default, tests annotated with {@code RestClientTest} will also auto-configure a 053 * {@link MockRestServiceServer}. For more fine-grained control the 054 * {@link AutoConfigureMockRestServiceServer @AutoConfigureMockRestServiceServer} 055 * annotation can be used. 056 * <p> 057 * If you are testing a bean that doesn't use {@link RestTemplateBuilder} but instead 058 * injects a {@link RestTemplate} directly, you can add 059 * {@code @AutoConfigureWebClient(registerRestTemplate=true)}. 060 * 061 * @author Stephane Nicoll 062 * @author Phillip Webb 063 * @author Artsiom Yudovin 064 * @since 1.4.0 065 */ 066@Target(ElementType.TYPE) 067@Retention(RetentionPolicy.RUNTIME) 068@Documented 069@Inherited 070@BootstrapWith(RestClientTestContextBootstrapper.class) 071@ExtendWith(SpringExtension.class) 072@OverrideAutoConfiguration(enabled = false) 073@TypeExcludeFilters(RestClientExcludeFilter.class) 074@AutoConfigureCache 075@AutoConfigureWebClient 076@AutoConfigureMockRestServiceServer 077@ImportAutoConfiguration 078public @interface RestClientTest { 079 080 /** 081 * Properties in form {@literal key=value} that should be added to the Spring 082 * {@link Environment} before the test runs. 083 * @return the properties to add 084 * @since 2.1.0 085 */ 086 String[] properties() default {}; 087 088 /** 089 * Specifies the components to test. This is an alias of {@link #components()} which 090 * can be used for brevity if no other attributes are defined. See 091 * {@link #components()} for details. 092 * @see #components() 093 * @return the components to test 094 */ 095 @AliasFor("components") 096 Class<?>[] value() default {}; 097 098 /** 099 * Specifies the components to test. May be left blank if components will be manually 100 * imported or created directly. 101 * @see #value() 102 * @return the components to test 103 */ 104 @AliasFor("value") 105 Class<?>[] components() default {}; 106 107 /** 108 * Determines if default filtering should be used with 109 * {@link SpringBootApplication @SpringBootApplication}. By default only 110 * {@code @JsonComponent} and {@code Module} beans are included. 111 * @see #includeFilters() 112 * @see #excludeFilters() 113 * @return if default filters should be used 114 */ 115 boolean useDefaultFilters() default true; 116 117 /** 118 * A set of include filters which can be used to add otherwise filtered beans to the 119 * application context. 120 * @return include filters to apply 121 */ 122 ComponentScan.Filter[] includeFilters() default {}; 123 124 /** 125 * A set of exclude filters which can be used to filter beans that would otherwise be 126 * added to the application context. 127 * @return exclude filters to apply 128 */ 129 ComponentScan.Filter[] excludeFilters() default {}; 130 131 /** 132 * Auto-configuration exclusions that should be applied for this test. 133 * @return auto-configuration exclusions to apply 134 */ 135 @AliasFor(annotation = ImportAutoConfiguration.class, attribute = "exclude") 136 Class<?>[] excludeAutoConfiguration() default {}; 137 138}