Переглянути джерело

mybatis-plus-boot-starter-test

miemie 5 роки тому
батько
коміт
9cf9750d6a

+ 16 - 0
mybatis-plus-boot-starter-test/build.gradle

@@ -0,0 +1,16 @@
+// 全量 copy 自 mybatis 的 mybatis-spring-boot-starter-test
+
+dependencies {
+    api(project(":mybatis-plus-boot-starter"))
+    implementation(enforcedPlatform("org.springframework.boot:spring-boot-dependencies:${springBootVersion}"))
+    api('org.springframework.boot:spring-boot-autoconfigure')
+    api('org.springframework.boot:spring-boot-test-autoconfigure')
+    api('org.springframework.boot:spring-boot-test')
+    api('org.springframework:spring-test') {
+        exclude module: "commons-logging"
+    }
+    api('org.springframework:spring-tx')
+    implementation("${lib.'junit-jupiter-api'}")
+    testCompile("${lib.h2}")
+    api('org.springframework.boot:spring-boot-starter-test')
+}

+ 20 - 0
mybatis-plus-boot-starter-test/src/main/java/com/baomidou/mybatisplus/test/autoconfigure/AutoConfigureMybatisPlus.java

@@ -0,0 +1,20 @@
+package com.baomidou.mybatisplus.test.autoconfigure;
+
+import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
+
+import java.lang.annotation.*;
+
+/**
+ * {@link ImportAutoConfiguration Auto-configuration imports} for typical Mybatis tests. Most tests should consider
+ * using {@link MybatisPlusTest @MybatisTest} rather than using this annotation directly.
+ *
+ * @author miemie
+ * @since 2020-05-27
+ */
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+@Inherited
+@ImportAutoConfiguration
+public @interface AutoConfigureMybatisPlus {
+}

+ 96 - 0
mybatis-plus-boot-starter-test/src/main/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusTest.java

@@ -0,0 +1,96 @@
+package com.baomidou.mybatisplus.test.autoconfigure;
+
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
+import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
+import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
+import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.annotation.ComponentScan.Filter;
+import org.springframework.core.annotation.AliasFor;
+import org.springframework.core.env.Environment;
+import org.springframework.test.context.BootstrapWith;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.lang.annotation.*;
+
+/**
+ * Annotation that can be used in combination with {@code @RunWith(SpringRunner.class)}(JUnit 4) and
+ * {@code @ExtendWith(SpringExtension.class)}(JUnit 5) for a typical mybatis test. Can be used when a test focuses
+ * <strong>only</strong> on mybatis-based components. Since 2.0.1, If you use this annotation on JUnit 5,
+ * {@code @ExtendWith(SpringExtension.class)} can omit on your test class.
+ * <p>
+ * Using this annotation will disable full auto-configuration and instead apply only configuration relevant to mybatis
+ * tests.
+ * <p>
+ * By default, tests annotated with {@code @MybatisTest} will use an embedded in-memory database (replacing any explicit
+ * or usually auto-configured DataSource). The {@link AutoConfigureTestDatabase @AutoConfigureTestDatabase} annotation
+ * can be used to override these settings.
+ * <p>
+ * If you are looking to load your full application configuration, but use an embedded database, you should consider
+ * {@link SpringBootTest @SpringBootTest} combined with {@link AutoConfigureTestDatabase @AutoConfigureTestDatabase}
+ * rather than this annotation.
+ *
+ * @author miemie
+ * @since 2020-05-27
+ */
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+@Inherited
+@BootstrapWith(MybatisPlusTestContextBootstrapper.class)
+@ExtendWith(SpringExtension.class)
+@OverrideAutoConfiguration(enabled = false)
+@TypeExcludeFilters(MybatisPlusTypeExcludeFilter.class)
+@Transactional
+@AutoConfigureCache
+@AutoConfigureMybatisPlus
+@AutoConfigureTestDatabase
+@ImportAutoConfiguration
+public @interface MybatisPlusTest {
+
+    /**
+     * Properties in form {@literal key=value} that should be added to the Spring {@link Environment} before the test
+     * runs.
+     *
+     * @return the properties to add
+     * @since 2.1.0
+     */
+    String[] properties() default {};
+
+    /**
+     * Determines if default filtering should be used with {@link SpringBootApplication @SpringBootApplication}. By
+     * default no beans are included.
+     *
+     * @return if default filters should be used
+     * @see #includeFilters()
+     * @see #excludeFilters()
+     */
+    boolean useDefaultFilters() default true;
+
+    /**
+     * A set of include filters which can be used to add otherwise filtered beans to the application context.
+     *
+     * @return include filters to apply
+     */
+    Filter[] includeFilters() default {};
+
+    /**
+     * A set of exclude filters which can be used to filter beans that would otherwise be added to the application
+     * context.
+     *
+     * @return exclude filters to apply
+     */
+    Filter[] excludeFilters() default {};
+
+    /**
+     * Auto-configuration exclusions that should be applied for this test.
+     *
+     * @return auto-configuration exclusions to apply
+     */
+    @AliasFor(annotation = ImportAutoConfiguration.class, attribute = "exclude")
+    Class<?>[] excludeAutoConfiguration() default {};
+}

+ 21 - 0
mybatis-plus-boot-starter-test/src/main/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusTestContextBootstrapper.java

@@ -0,0 +1,21 @@
+package com.baomidou.mybatisplus.test.autoconfigure;
+
+import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
+import org.springframework.core.annotation.AnnotatedElementUtils;
+import org.springframework.test.context.TestContextBootstrapper;
+
+/**
+ * {@link TestContextBootstrapper} for {@link MybatisPlusTest @MybatisTest} support.
+ *
+ * @author miemie
+ * @since 2020-05-27
+ */
+class MybatisPlusTestContextBootstrapper extends SpringBootTestContextBootstrapper {
+
+    @Override
+    protected String[] getProperties(Class<?> testClass) {
+        MybatisPlusTest annotation = AnnotatedElementUtils.getMergedAnnotation(testClass, MybatisPlusTest.class);
+        return (annotation != null) ? annotation.properties() : null;
+    }
+
+}

+ 55 - 0
mybatis-plus-boot-starter-test/src/main/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusTypeExcludeFilter.java

@@ -0,0 +1,55 @@
+package com.baomidou.mybatisplus.test.autoconfigure;
+
+import org.springframework.boot.context.TypeExcludeFilter;
+import org.springframework.boot.test.autoconfigure.filter.AnnotationCustomizableTypeExcludeFilter;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.core.annotation.AnnotatedElementUtils;
+
+import java.util.Collections;
+import java.util.Set;
+
+/**
+ * {@link TypeExcludeFilter} for {@link MybatisPlusTest @MybatisPlusTest}.
+ *
+ * @author miemie
+ * @since 2020-05-27
+ */
+class MybatisPlusTypeExcludeFilter extends AnnotationCustomizableTypeExcludeFilter {
+    private final MybatisPlusTest annotation;
+
+    MybatisPlusTypeExcludeFilter(Class<?> testClass) {
+        this.annotation = AnnotatedElementUtils.getMergedAnnotation(testClass, MybatisPlusTest.class);
+    }
+
+    @Override
+    protected boolean hasAnnotation() {
+        return this.annotation != null;
+    }
+
+    @Override
+    protected ComponentScan.Filter[] getFilters(FilterType type) {
+        switch (type) {
+            case INCLUDE:
+                return this.annotation.includeFilters();
+            case EXCLUDE:
+                return this.annotation.excludeFilters();
+            default:
+                throw new IllegalStateException("Unsupported type " + type);
+        }
+    }
+
+    @Override
+    protected boolean isUseDefaultFilters() {
+        return this.annotation.useDefaultFilters();
+    }
+
+    @Override
+    protected Set<Class<?>> getDefaultIncludes() {
+        return Collections.emptySet();
+    }
+
+    @Override
+    protected Set<Class<?>> getComponentIncludes() {
+        return Collections.emptySet();
+    }
+}

+ 10 - 0
mybatis-plus-boot-starter-test/src/main/resources/META-INF/spring.factories

@@ -0,0 +1,10 @@
+# AutoConfigureMybatis auto-configuration imports
+com.baomidou.mybatisplus.test.autoconfigure.AutoConfigureMybatisPlus=\
+org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
+org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
+org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
+org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\
+org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
+org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\
+com.baomidou.mybatisplus.autoconfigure.MybatisPlusLanguageDriverAutoConfiguration,\
+com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration

+ 25 - 0
mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusSampleTest.java

@@ -0,0 +1,25 @@
+package com.baomidou.mybatisplus.test.autoconfigure;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * @author miemie
+ * @since 2020-05-27
+ */
+//@MybatisPlusTest(properties = "spring.datasource.schema=classpath:schema.sql")
+@MybatisPlusTest
+class MybatisPlusSampleTest {
+
+    @Autowired
+    private SampleMapper sampleMapper;
+
+    @Test
+    void testInsert() {
+        Sample sample = new Sample();
+        sampleMapper.insert(sample);
+        assertThat(sample.getId()).isNotNull();
+    }
+}

+ 12 - 0
mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/MybatisPlusTestApplication.java

@@ -0,0 +1,12 @@
+package com.baomidou.mybatisplus.test.autoconfigure;
+
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+/**
+ * @author miemie
+ * @since 2020-05-27
+ */
+@SpringBootApplication
+public class MybatisPlusTestApplication {
+
+}

+ 13 - 0
mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/Sample.java

@@ -0,0 +1,13 @@
+package com.baomidou.mybatisplus.test.autoconfigure;
+
+import lombok.Data;
+
+/**
+ * @author miemie
+ * @since 2020-05-27
+ */
+@Data
+public class Sample {
+    private Long id;
+    private String name;
+}

+ 12 - 0
mybatis-plus-boot-starter-test/src/test/java/com/baomidou/mybatisplus/test/autoconfigure/SampleMapper.java

@@ -0,0 +1,12 @@
+package com.baomidou.mybatisplus.test.autoconfigure;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Mapper;
+
+/**
+ * @author miemie
+ * @since 2020-05-27
+ */
+@Mapper
+public interface SampleMapper extends BaseMapper<Sample> {
+}

+ 8 - 0
mybatis-plus-boot-starter-test/src/test/resources/application.yml

@@ -0,0 +1,8 @@
+spring:
+    datasource:
+        schema: classpath:schema.sql
+
+logging:
+    level:
+        org.springframework.jdbc: debug
+        com.baomidou.mybatisplus.test.autoconfigure: debug

+ 6 - 0
mybatis-plus-boot-starter-test/src/test/resources/schema.sql

@@ -0,0 +1,6 @@
+drop table if exists sample;
+create table if not exists sample
+(
+    id   bigint,
+    name varchar
+);

+ 2 - 0
settings.gradle

@@ -5,3 +5,5 @@ include 'mybatis-plus-annotation'
 include 'mybatis-plus-extension'
 include 'mybatis-plus-generator'
 include 'mybatis-plus-boot-starter'
+include 'mybatis-plus-boot-starter-test'
+