Skip to content

Commit a294e28

Browse files
authored
Merge pull request #148 from kazuki43zoo/polishing-test-on-autoconfigure-test
Polishing test for mybatis-spring-boot-test-autoconfigure
2 parents 02e70db + 61cd300 commit a294e28

File tree

10 files changed

+183
-51
lines changed

10 files changed

+183
-51
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,21 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.mybatis.spring.boot.test.autoconfigure;
1817

1918
import org.springframework.stereotype.Component;
2019

2120
/**
22-
* Example component used with {@link MybatisTest} tests.
21+
* Example component that annotated {@link Component @Component} used with {@link MybatisTest} tests.
2322
*
2423
* @author wonwoo
2524
* @since 1.2.1
2625
*/
2726
@Component
2827
public class ExampleComponent {
2928

29+
public String getMessage() {
30+
return "Hello!";
31+
}
32+
3033
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.mybatis.spring.boot.test.autoconfigure;
1817

1918
import org.springframework.boot.autoconfigure.SpringBootApplication;
19+
import org.springframework.cache.annotation.EnableCaching;
2020

2121
/**
2222
* Example {@link SpringBootApplication} used with {@link MybatisTest} tests.
@@ -25,6 +25,7 @@
2525
* @since 1.2.1
2626
*/
2727
@SpringBootApplication
28+
@EnableCaching
2829
class ExampleMybatisApplication {
2930

3031
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright 2015-2017 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.stereotype.Service;
19+
20+
/**
21+
* Example component that annotated {@link Service @Service} used with {@link MybatisTest} tests.
22+
*
23+
* @author Kazuki Shimizu
24+
* @since 1.2.1
25+
*/
26+
@Service
27+
public class ExampleService {
28+
29+
public String getMessage() {
30+
return "Goodbye!";
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright 2015-2017 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.junit.Test;
19+
import org.junit.runner.RunWith;
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.context.annotation.ComponentScan;
22+
import org.springframework.stereotype.Component;
23+
import org.springframework.stereotype.Service;
24+
import org.springframework.test.context.TestPropertySource;
25+
import org.springframework.test.context.junit4.SpringRunner;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Integration test with custom filter for {@link MybatisTest}.
31+
*
32+
* @author Kazuki Shimizu
33+
* @since 1.2.1
34+
*/
35+
@RunWith(SpringRunner.class)
36+
@MybatisTest(includeFilters = @ComponentScan.Filter(Component.class), excludeFilters = @ComponentScan.Filter(Service.class))
37+
@TestPropertySource(properties = {
38+
"mybatis.type-aliases-package=org.mybatis.spring.boot.test.autoconfigure"
39+
})
40+
public class MybatisTestCustomFilterIntegrationTest {
41+
42+
@Autowired
43+
private ExampleComponent component;
44+
45+
@Autowired(required = false)
46+
private ExampleService service;
47+
48+
@Test
49+
public void testIncludeFilter() {
50+
assertThat(component.getMessage()).isEqualTo("Hello!");
51+
}
52+
53+
@Test
54+
public void testExcludeFilter() {
55+
assertThat(service).isNull();
56+
}
57+
58+
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
/**
2-
* Copyright 2015-2017 the original author or authors.
3-
* <p>
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-
* <p>
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
* <p>
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.
2+
* Copyright 2015-2017 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.
1515
*/
16-
1716
package org.mybatis.spring.boot.test.autoconfigure;
1817

1918
import java.util.HashMap;
@@ -27,9 +26,15 @@
2726

2827
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2928
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.cache.CacheManager;
30+
import org.springframework.cache.interceptor.CacheInterceptor;
3031
import org.springframework.context.ApplicationContext;
32+
import org.springframework.jdbc.core.JdbcTemplate;
33+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
34+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
3135
import org.springframework.test.context.TestPropertySource;
3236
import org.springframework.test.context.junit4.SpringRunner;
37+
import org.springframework.transaction.interceptor.TransactionInterceptor;
3338

3439
import static org.assertj.core.api.Assertions.assertThat;
3540

@@ -43,10 +48,10 @@
4348
@MybatisTest
4449
@TestPropertySource(properties = {
4550
"mybatis.type-aliases-package=org.mybatis.spring.boot.test.autoconfigure",
46-
"mybatis.mapper-locations=org/mybatis/spring/boot/test/autoconfigure/*.xml",
51+
"logging.level.org.springframework.jdbc=debug",
4752
"spring.datasource.schema=classpath:org/mybatis/spring/boot/test/autoconfigure/schema.sql"
4853
})
49-
public class MybatisTestIntegrationTests {
54+
public class MybatisTestIntegrationTest {
5055

5156
@Rule
5257
public ExpectedException thrown = ExpectedException.none();
@@ -60,16 +65,6 @@ public class MybatisTestIntegrationTests {
6065
@Autowired
6166
private ApplicationContext applicationContext;
6267

63-
@Test
64-
public void sqlSessionIsNotNullTest() {
65-
assertThat(sqlSession).isNotNull();
66-
}
67-
68-
@Test
69-
public void mapperIsNotNullTest() {
70-
assertThat(sampleMapper).isNotNull();
71-
}
72-
7368
@Test
7469
public void testSqlSession() {
7570
Map<String, Object> parameters = new HashMap<String, Object>();
@@ -87,11 +82,23 @@ public void testMapper() {
8782
parameters.put("id", 1);
8883
parameters.put("name", "wonwoo");
8984
sqlSession.insert("saveSample", parameters);
90-
Sample sample = sampleMapper.findByname("wonwoo");
85+
Sample sample = sampleMapper.findByName("wonwoo");
9186
assertThat(sample.getId()).isNotNull().isEqualTo(1L);
9287
assertThat(sample.getName()).isNotNull().isEqualTo("wonwoo");
9388
}
9489

90+
@Test
91+
public void testAutoConfigureComponents() {
92+
// @AutoConfigureMybatis
93+
this.applicationContext.getBean(JdbcTemplate.class);
94+
this.applicationContext.getBean(NamedParameterJdbcTemplate.class);
95+
this.applicationContext.getBean(DataSourceTransactionManager.class);
96+
this.applicationContext.getBean(TransactionInterceptor.class);
97+
// @AutoConfigureCache
98+
this.applicationContext.getBean(CacheManager.class);
99+
this.applicationContext.getBean(CacheInterceptor.class);
100+
}
101+
95102
@Test
96103
public void didNotInjectExampleComponent() {
97104
this.thrown.expect(NoSuchBeanDefinitionException.class);

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

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.mybatis.spring.boot.test.autoconfigure;
1817

1918
/**

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.mybatis.spring.boot.test.autoconfigure;
1817

1918
import org.apache.ibatis.annotations.Mapper;
@@ -27,6 +26,6 @@
2726
@Mapper
2827
public interface SampleMapper {
2928

30-
Sample findByname(String name);
29+
Sample findByName(String name);
3130

3231
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
4+
Copyright 2015-2017 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<!DOCTYPE mapper
20+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
21+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
22+
<mapper namespace="org.mybatis.spring.boot.test.autoconfigure.SampleMapper">
23+
<select id="findSample" resultType="Sample">
24+
select * from sample where id = #{id}
25+
</select>
26+
27+
<select id="findByName" resultType="Sample">
28+
select * from sample where name = #{name}
29+
</select>
30+
31+
<insert id="saveSample">
32+
insert into sample values(#{id}, #{name})
33+
</insert>
34+
</mapper>

mybatis-spring-boot-test-autoconfigure/src/test/resources/org/mybatis/spring/boot/test/autoconfigure/sample.xml

-18
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1+
--
2+
-- Copyright 2015-2017 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+
117
create table sample (id int, name varchar);

0 commit comments

Comments
 (0)