Skip to content

Commit 9cf9750

Browse files
committed
mybatis-plus-boot-starter-test
1 parent bd7f0da commit 9cf9750

File tree

13 files changed

+296
-0
lines changed

13 files changed

+296
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// 全量 copy 自 mybatis 的 mybatis-spring-boot-starter-test
2+
3+
dependencies {
4+
api(project(":mybatis-plus-boot-starter"))
5+
implementation(enforcedPlatform("org.springframework.boot:spring-boot-dependencies:${springBootVersion}"))
6+
api('org.springframework.boot:spring-boot-autoconfigure')
7+
api('org.springframework.boot:spring-boot-test-autoconfigure')
8+
api('org.springframework.boot:spring-boot-test')
9+
api('org.springframework:spring-test') {
10+
exclude module: "commons-logging"
11+
}
12+
api('org.springframework:spring-tx')
13+
implementation("${lib.'junit-jupiter-api'}")
14+
testCompile("${lib.h2}")
15+
api('org.springframework.boot:spring-boot-starter-test')
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baomidou.mybatisplus.test.autoconfigure;
2+
3+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
4+
5+
import java.lang.annotation.*;
6+
7+
/**
8+
* {@link ImportAutoConfiguration Auto-configuration imports} for typical Mybatis tests. Most tests should consider
9+
* using {@link MybatisPlusTest @MybatisTest} rather than using this annotation directly.
10+
*
11+
* @author miemie
12+
* @since 2020-05-27
13+
*/
14+
@Target(ElementType.TYPE)
15+
@Retention(RetentionPolicy.RUNTIME)
16+
@Documented
17+
@Inherited
18+
@ImportAutoConfiguration
19+
public @interface AutoConfigureMybatisPlus {
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baomidou.mybatisplus.test.autoconfigure;
2+
3+
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
4+
import org.springframework.core.annotation.AnnotatedElementUtils;
5+
import org.springframework.test.context.TestContextBootstrapper;
6+
7+
/**
8+
* {@link TestContextBootstrapper} for {@link MybatisPlusTest @MybatisTest} support.
9+
*
10+
* @author miemie
11+
* @since 2020-05-27
12+
*/
13+
class MybatisPlusTestContextBootstrapper extends SpringBootTestContextBootstrapper {
14+
15+
@Override
16+
protected String[] getProperties(Class<?> testClass) {
17+
MybatisPlusTest annotation = AnnotatedElementUtils.getMergedAnnotation(testClass, MybatisPlusTest.class);
18+
return (annotation != null) ? annotation.properties() : null;
19+
}
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baomidou.mybatisplus.test.autoconfigure;
2+
3+
import org.springframework.boot.context.TypeExcludeFilter;
4+
import org.springframework.boot.test.autoconfigure.filter.AnnotationCustomizableTypeExcludeFilter;
5+
import org.springframework.context.annotation.ComponentScan;
6+
import org.springframework.core.annotation.AnnotatedElementUtils;
7+
8+
import java.util.Collections;
9+
import java.util.Set;
10+
11+
/**
12+
* {@link TypeExcludeFilter} for {@link MybatisPlusTest @MybatisPlusTest}.
13+
*
14+
* @author miemie
15+
* @since 2020-05-27
16+
*/
17+
class MybatisPlusTypeExcludeFilter extends AnnotationCustomizableTypeExcludeFilter {
18+
private final MybatisPlusTest annotation;
19+
20+
MybatisPlusTypeExcludeFilter(Class<?> testClass) {
21+
this.annotation = AnnotatedElementUtils.getMergedAnnotation(testClass, MybatisPlusTest.class);
22+
}
23+
24+
@Override
25+
protected boolean hasAnnotation() {
26+
return this.annotation != null;
27+
}
28+
29+
@Override
30+
protected ComponentScan.Filter[] getFilters(FilterType type) {
31+
switch (type) {
32+
case INCLUDE:
33+
return this.annotation.includeFilters();
34+
case EXCLUDE:
35+
return this.annotation.excludeFilters();
36+
default:
37+
throw new IllegalStateException("Unsupported type " + type);
38+
}
39+
}
40+
41+
@Override
42+
protected boolean isUseDefaultFilters() {
43+
return this.annotation.useDefaultFilters();
44+
}
45+
46+
@Override
47+
protected Set<Class<?>> getDefaultIncludes() {
48+
return Collections.emptySet();
49+
}
50+
51+
@Override
52+
protected Set<Class<?>> getComponentIncludes() {
53+
return Collections.emptySet();
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# AutoConfigureMybatis auto-configuration imports
2+
com.baomidou.mybatisplus.test.autoconfigure.AutoConfigureMybatisPlus=\
3+
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
4+
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
5+
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
6+
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\
7+
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
8+
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\
9+
com.baomidou.mybatisplus.autoconfigure.MybatisPlusLanguageDriverAutoConfiguration,\
10+
com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baomidou.mybatisplus.test.autoconfigure;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
8+
/**
9+
* @author miemie
10+
* @since 2020-05-27
11+
*/
12+
//@MybatisPlusTest(properties = "spring.datasource.schema=classpath:schema.sql")
13+
@MybatisPlusTest
14+
class MybatisPlusSampleTest {
15+
16+
@Autowired
17+
private SampleMapper sampleMapper;
18+
19+
@Test
20+
void testInsert() {
21+
Sample sample = new Sample();
22+
sampleMapper.insert(sample);
23+
assertThat(sample.getId()).isNotNull();
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baomidou.mybatisplus.test.autoconfigure;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
5+
/**
6+
* @author miemie
7+
* @since 2020-05-27
8+
*/
9+
@SpringBootApplication
10+
public class MybatisPlusTestApplication {
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baomidou.mybatisplus.test.autoconfigure;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* @author miemie
7+
* @since 2020-05-27
8+
*/
9+
@Data
10+
public class Sample {
11+
private Long id;
12+
private String name;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baomidou.mybatisplus.test.autoconfigure;
2+
3+
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4+
import org.apache.ibatis.annotations.Mapper;
5+
6+
/**
7+
* @author miemie
8+
* @since 2020-05-27
9+
*/
10+
@Mapper
11+
public interface SampleMapper extends BaseMapper<Sample> {
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
spring:
2+
datasource:
3+
schema: classpath:schema.sql
4+
5+
logging:
6+
level:
7+
org.springframework.jdbc: debug
8+
com.baomidou.mybatisplus.test.autoconfigure: debug
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
drop table if exists sample;
2+
create table if not exists sample
3+
(
4+
id bigint,
5+
name varchar
6+
);

settings.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ include 'mybatis-plus-annotation'
55
include 'mybatis-plus-extension'
66
include 'mybatis-plus-generator'
77
include 'mybatis-plus-boot-starter'
8+
include 'mybatis-plus-boot-starter-test'
9+

0 commit comments

Comments
 (0)