diff --git a/mybatis-spring-boot-test-autoconfigure/src/main/java/org/mybatis/spring/boot/test/autoconfigure/MybatisTest.java b/mybatis-spring-boot-test-autoconfigure/src/main/java/org/mybatis/spring/boot/test/autoconfigure/MybatisTest.java index c41de5c80..8c6e3c2a3 100644 --- a/mybatis-spring-boot-test-autoconfigure/src/main/java/org/mybatis/spring/boot/test/autoconfigure/MybatisTest.java +++ b/mybatis-spring-boot-test-autoconfigure/src/main/java/org/mybatis/spring/boot/test/autoconfigure/MybatisTest.java @@ -31,9 +31,9 @@ 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.boot.test.context.SpringBootTestContextBootstrapper; 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; @@ -63,7 +63,7 @@ @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited -@BootstrapWith(SpringBootTestContextBootstrapper.class) +@BootstrapWith(MybatisTestContextBootstrapper.class) @ExtendWith(SpringExtension.class) @OverrideAutoConfiguration(enabled = false) @TypeExcludeFilters(MybatisTypeExcludeFilter.class) @@ -74,6 +74,15 @@ @ImportAutoConfiguration public @interface MybatisTest { + /** + * 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. diff --git a/mybatis-spring-boot-test-autoconfigure/src/main/java/org/mybatis/spring/boot/test/autoconfigure/MybatisTestContextBootstrapper.java b/mybatis-spring-boot-test-autoconfigure/src/main/java/org/mybatis/spring/boot/test/autoconfigure/MybatisTestContextBootstrapper.java new file mode 100644 index 000000000..d397430c6 --- /dev/null +++ b/mybatis-spring-boot-test-autoconfigure/src/main/java/org/mybatis/spring/boot/test/autoconfigure/MybatisTestContextBootstrapper.java @@ -0,0 +1,36 @@ +/** + * Copyright 2015-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mybatis.spring.boot.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 MybatisTest @MybatisTest} support. + * + * @author Kazuki Shimizu + * @since 2.1.0 + */ +class MybatisTestContextBootstrapper extends SpringBootTestContextBootstrapper { + + @Override + protected String[] getProperties(Class testClass) { + MybatisTest annotation = AnnotatedElementUtils.getMergedAnnotation(testClass, MybatisTest.class); + return (annotation != null) ? annotation.properties() : null; + } + +} diff --git a/mybatis-spring-boot-test-autoconfigure/src/test/java/org/mybatis/spring/boot/test/autoconfigure/MybatisTestIntegrationTest.java b/mybatis-spring-boot-test-autoconfigure/src/test/java/org/mybatis/spring/boot/test/autoconfigure/MybatisTestIntegrationTest.java index bbb1b3049..8e493feaa 100755 --- a/mybatis-spring-boot-test-autoconfigure/src/test/java/org/mybatis/spring/boot/test/autoconfigure/MybatisTestIntegrationTest.java +++ b/mybatis-spring-boot-test-autoconfigure/src/test/java/org/mybatis/spring/boot/test/autoconfigure/MybatisTestIntegrationTest.java @@ -23,7 +23,6 @@ import org.apache.ibatis.session.SqlSession; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.CacheManager; @@ -32,8 +31,6 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; -import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.transaction.interceptor.TransactionInterceptor; /** @@ -42,8 +39,7 @@ * @author wonwoo * @since 1.2.1 */ -@MybatisTest -@TestPropertySource(properties = { "mybatis.type-aliases-package=org.mybatis.spring.boot.test.autoconfigure", +@MybatisTest(properties = { "mybatis.type-aliases-package=org.mybatis.spring.boot.test.autoconfigure", "logging.level.org.springframework.jdbc=debug", "spring.datasource.schema=classpath:org/mybatis/spring/boot/test/autoconfigure/schema.sql" }) class MybatisTestIntegrationTest {