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.jdbc;
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.context.SpringBootTest;
034import org.springframework.context.annotation.ComponentScan;
035import org.springframework.core.annotation.AliasFor;
036import org.springframework.core.env.Environment;
037import org.springframework.test.context.BootstrapWith;
038import org.springframework.test.context.junit.jupiter.SpringExtension;
039import org.springframework.transaction.annotation.Transactional;
040
041/**
042 * Annotation that can be used in combination with {@code @RunWith(SpringRunner.class)}
043 * for a typical jdbc test. Can be used when a test focuses <strong>only</strong> on
044 * jdbc-based components.
045 * <p>
046 * Using this annotation will disable full auto-configuration and instead apply only
047 * configuration relevant to jdbc tests.
048 * <p>
049 * By default, tests annotated with {@code @JdbcTest} will use an embedded in-memory
050 * database (replacing any explicit or usually auto-configured DataSource). The
051 * {@link AutoConfigureTestDatabase @AutoConfigureTestDatabase} annotation can be used to
052 * override these settings.
053 * <p>
054 * If you are looking to load your full application configuration, but use an embedded
055 * database, you should consider {@link SpringBootTest @SpringBootTest} combined with
056 * {@link AutoConfigureTestDatabase @AutoConfigureTestDatabase} rather than this
057 * annotation.
058 *
059 * @author Stephane Nicoll
060 * @author Artsiom Yudovin
061 * @see AutoConfigureJdbc
062 * @see AutoConfigureTestDatabase
063 * @see AutoConfigureCache
064 */
065@Target(ElementType.TYPE)
066@Retention(RetentionPolicy.RUNTIME)
067@Documented
068@Inherited
069@BootstrapWith(JdbcTestContextBootstrapper.class)
070@ExtendWith(SpringExtension.class)
071@OverrideAutoConfiguration(enabled = false)
072@TypeExcludeFilters(JdbcTypeExcludeFilter.class)
073@Transactional
074@AutoConfigureCache
075@AutoConfigureJdbc
076@AutoConfigureTestDatabase
077@ImportAutoConfiguration
078public @interface JdbcTest {
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         * Determines if default filtering should be used with
090         * {@link SpringBootApplication @SpringBootApplication}. By default no beans are
091         * included.
092         * @see #includeFilters()
093         * @see #excludeFilters()
094         * @return if default filters should be used
095         */
096        boolean useDefaultFilters() default true;
097
098        /**
099         * A set of include filters which can be used to add otherwise filtered beans to the
100         * application context.
101         * @return include filters to apply
102         */
103        ComponentScan.Filter[] includeFilters() default {};
104
105        /**
106         * A set of exclude filters which can be used to filter beans that would otherwise be
107         * added to the application context.
108         * @return exclude filters to apply
109         */
110        ComponentScan.Filter[] excludeFilters() default {};
111
112        /**
113         * Auto-configuration exclusions that should be applied for this test.
114         * @return auto-configuration exclusions to apply
115         */
116        @AliasFor(annotation = ImportAutoConfiguration.class, attribute = "exclude")
117        Class<?>[] excludeAutoConfiguration() default {};
118
119}