View Javadoc
1   /*
2    *    Copyright 2015-2022 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  package org.mybatis.spring.boot.test.autoconfigure;
17  
18  import java.lang.annotation.Documented;
19  import java.lang.annotation.ElementType;
20  import java.lang.annotation.Inherited;
21  import java.lang.annotation.Retention;
22  import java.lang.annotation.RetentionPolicy;
23  import java.lang.annotation.Target;
24  
25  import org.junit.jupiter.api.extension.ExtendWith;
26  import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
27  import org.springframework.boot.autoconfigure.SpringBootApplication;
28  import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
29  import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
30  import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
31  import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
32  import org.springframework.boot.test.context.SpringBootTest;
33  import org.springframework.context.annotation.ComponentScan.Filter;
34  import org.springframework.core.annotation.AliasFor;
35  import org.springframework.core.env.Environment;
36  import org.springframework.test.context.BootstrapWith;
37  import org.springframework.test.context.junit.jupiter.SpringExtension;
38  import org.springframework.transaction.annotation.Transactional;
39  
40  /**
41   * Annotation that can be used in combination with {@code @RunWith(SpringRunner.class)}(JUnit 4) and
42   * {@code @ExtendWith(SpringExtension.class)}(JUnit 5) for a typical mybatis test. Can be used when a test focuses
43   * <strong>only</strong> on mybatis-based components. Since 2.0.1, If you use this annotation on JUnit 5,
44   * {@code @ExtendWith(SpringExtension.class)} can omit on your test class.
45   * <p>
46   * Using this annotation will disable full auto-configuration and instead apply only configuration relevant to mybatis
47   * tests.
48   * <p>
49   * By default, tests annotated with {@code @MybatisTest} will use an embedded in-memory database (replacing any explicit
50   * or usually auto-configured DataSource). The {@link AutoConfigureTestDatabase @AutoConfigureTestDatabase} annotation
51   * can be used to override these settings.
52   * <p>
53   * If you are looking to load your full application configuration, but use an embedded database, you should consider
54   * {@link SpringBootTest @SpringBootTest} combined with {@link AutoConfigureTestDatabase @AutoConfigureTestDatabase}
55   * rather than this annotation.
56   *
57   * @author wonwoo
58   *
59   * @see AutoConfigureMybatis
60   *
61   * @since 1.2.1
62   */
63  @Target(ElementType.TYPE)
64  @Retention(RetentionPolicy.RUNTIME)
65  @Documented
66  @Inherited
67  @BootstrapWith(MybatisTestContextBootstrapper.class)
68  @ExtendWith(SpringExtension.class)
69  @OverrideAutoConfiguration(enabled = false)
70  @TypeExcludeFilters(MybatisTypeExcludeFilter.class)
71  @Transactional
72  @AutoConfigureCache
73  @AutoConfigureMybatis
74  @AutoConfigureTestDatabase
75  @ImportAutoConfiguration
76  public @interface MybatisTest {
77  
78    /**
79     * Properties in form {@literal key=value} that should be added to the Spring {@link Environment} before the test
80     * runs.
81     *
82     * @return the properties to add
83     *
84     * @since 2.1.0
85     */
86    String[] properties() default {};
87  
88    /**
89     * Determines if default filtering should be used with {@link SpringBootApplication @SpringBootApplication}. By
90     * default no beans are included.
91     *
92     * @return if default filters should be used
93     *
94     * @see #includeFilters()
95     * @see #excludeFilters()
96     */
97    boolean useDefaultFilters() default true;
98  
99    /**
100    * A set of include filters which can be used to add otherwise filtered beans to the application context.
101    *
102    * @return include filters to apply
103    */
104   Filter[] includeFilters() default {};
105 
106   /**
107    * A set of exclude filters which can be used to filter beans that would otherwise be added to the application
108    * context.
109    *
110    * @return exclude filters to apply
111    */
112   Filter[] excludeFilters() default {};
113 
114   /**
115    * Auto-configuration exclusions that should be applied for this test.
116    *
117    * @return auto-configuration exclusions to apply
118    */
119   @AliasFor(annotation = ImportAutoConfiguration.class, attribute = "exclude")
120   Class<?>[] excludeAutoConfiguration() default {};
121 
122 }