Skip to content

Commit dbeac87

Browse files
committed
Add 'properties' attribute on MybatisTest annotation
Fixes gh-362
1 parent 40d2f13 commit dbeac87

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

mybatis-spring-boot-test-autoconfigure/src/main/java/org/mybatis/spring/boot/test/autoconfigure/MybatisTest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
3232
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
3333
import org.springframework.boot.test.context.SpringBootTest;
34-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
3534
import org.springframework.context.annotation.ComponentScan.Filter;
3635
import org.springframework.core.annotation.AliasFor;
36+
import org.springframework.core.env.Environment;
3737
import org.springframework.test.context.BootstrapWith;
3838
import org.springframework.test.context.junit.jupiter.SpringExtension;
3939
import org.springframework.transaction.annotation.Transactional;
@@ -63,7 +63,7 @@
6363
@Retention(RetentionPolicy.RUNTIME)
6464
@Documented
6565
@Inherited
66-
@BootstrapWith(SpringBootTestContextBootstrapper.class)
66+
@BootstrapWith(MybatisTestContextBootstrapper.class)
6767
@ExtendWith(SpringExtension.class)
6868
@OverrideAutoConfiguration(enabled = false)
6969
@TypeExcludeFilters(MybatisTypeExcludeFilter.class)
@@ -74,6 +74,15 @@
7474
@ImportAutoConfiguration
7575
public @interface MybatisTest {
7676

77+
/**
78+
* Properties in form {@literal key=value} that should be added to the Spring {@link Environment} before the test
79+
* runs.
80+
*
81+
* @return the properties to add
82+
* @since 2.1.0
83+
*/
84+
String[] properties() default {};
85+
7786
/**
7887
* Determines if default filtering should be used with {@link SpringBootApplication @SpringBootApplication}. By
7988
* default no beans are included.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright 2015-2019 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+
* http://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 org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
19+
import org.springframework.core.annotation.AnnotatedElementUtils;
20+
import org.springframework.test.context.TestContextBootstrapper;
21+
22+
/**
23+
* {@link TestContextBootstrapper} for {@link MybatisTest @MybatisTest} support.
24+
*
25+
* @author Kazuki Shimizu
26+
* @since 2.1.0
27+
*/
28+
class MybatisTestContextBootstrapper extends SpringBootTestContextBootstrapper {
29+
30+
@Override
31+
protected String[] getProperties(Class<?> testClass) {
32+
MybatisTest annotation = AnnotatedElementUtils.getMergedAnnotation(testClass, MybatisTest.class);
33+
return (annotation != null) ? annotation.properties() : null;
34+
}
35+
36+
}

mybatis-spring-boot-test-autoconfigure/src/test/java/org/mybatis/spring/boot/test/autoconfigure/MybatisTestIntegrationTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.apache.ibatis.session.SqlSession;
2424
import org.junit.jupiter.api.Assertions;
2525
import org.junit.jupiter.api.Test;
26-
import org.junit.jupiter.api.extension.ExtendWith;
2726
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2827
import org.springframework.beans.factory.annotation.Autowired;
2928
import org.springframework.cache.CacheManager;
@@ -32,8 +31,6 @@
3231
import org.springframework.jdbc.core.JdbcTemplate;
3332
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
3433
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
35-
import org.springframework.test.context.TestPropertySource;
36-
import org.springframework.test.context.junit.jupiter.SpringExtension;
3734
import org.springframework.transaction.interceptor.TransactionInterceptor;
3835

3936
/**
@@ -42,8 +39,7 @@
4239
* @author wonwoo
4340
* @since 1.2.1
4441
*/
45-
@MybatisTest
46-
@TestPropertySource(properties = { "mybatis.type-aliases-package=org.mybatis.spring.boot.test.autoconfigure",
42+
@MybatisTest(properties = { "mybatis.type-aliases-package=org.mybatis.spring.boot.test.autoconfigure",
4743
"logging.level.org.springframework.jdbc=debug",
4844
"spring.datasource.schema=classpath:org/mybatis/spring/boot/test/autoconfigure/schema.sql" })
4945
class MybatisTestIntegrationTest {

0 commit comments

Comments
 (0)