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