Skip to content

Commit 02e70db

Browse files
authored
Merge pull request #147 from kazuki43zoo/refactoring-test-on-sample
Polishing test on sample applications
2 parents bdfd308 + 85c782c commit 02e70db

File tree

8 files changed

+160
-35
lines changed

8 files changed

+160
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
import org.springframework.boot.autoconfigure.SpringBootApplication;
2323

2424
@SpringBootApplication
25-
public class SampleMapperApplication implements CommandLineRunner {
25+
public class SampleMybatisApplication implements CommandLineRunner {
2626

2727
public static void main(String[] args) {
28-
SpringApplication.run(SampleMapperApplication.class, args);
28+
SpringApplication.run(SampleMybatisApplication.class, args);
2929
}
3030

3131
final private CityMapper cityMapper;
3232

33-
public SampleMapperApplication(CityMapper cityMapper) {
33+
public SampleMybatisApplication(CityMapper cityMapper) {
3434
this.cityMapper = cityMapper;
3535
}
3636

+4-9
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,36 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package sample.mybatis;
16+
package sample.mybatis.mapper;
1717

1818
import org.junit.Test;
1919
import org.junit.runner.RunWith;
2020
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
2121
import sample.mybatis.domain.City;
22-
import sample.mybatis.mapper.CityMapper;
2322

2423
import org.springframework.beans.factory.annotation.Autowired;
2524
import org.springframework.test.context.junit4.SpringRunner;
2625

2726
import static org.assertj.core.api.Assertions.assertThat;
2827

2928
/**
30-
*
29+
* Tests for {@link CityMapper}.
3130
* @author wonwoo
3231
* @since 1.2.1
3332
*/
3433
@RunWith(SpringRunner.class)
3534
@MybatisTest
36-
public class SampleMapperTest {
35+
public class CityMapperTest {
3736

3837
@Autowired
3938
private CityMapper cityMapper;
4039

41-
@Test
42-
public void mapperIsNotNullTest() {
43-
assertThat(cityMapper).isNotNull();
44-
}
45-
4640
@Test
4741
public void findByStateTest() {
4842
City city = cityMapper.findByState("CA");
4943
assertThat(city.getName()).isEqualTo("San Francisco");
5044
assertThat(city.getState()).isEqualTo("CA");
5145
assertThat(city.getCountry()).isEqualTo("US");
5246
}
47+
5348
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 sample.mybatis.mapper;
17+
18+
import org.springframework.boot.autoconfigure.SpringBootApplication;
19+
20+
/**
21+
* The Spring Boot Application for testing {@link org.mybatis.spring.boot.test.autoconfigure.MybatisTest @MybatisTest}.
22+
* <p>
23+
* This class has role for prevent to run the {@link sample.mybatis.SampleMybatisApplication}.
24+
* For more detail information, please refer <a href="http://stackoverflow.com/questions/42722480/jdbctest-detect-class-annotated-springbootapplication">Here</a>.
25+
*
26+
* @author Kazuki Shimizu
27+
* @since 1.2.1
28+
*/
29+
@SpringBootApplication
30+
public class MapperTestApplication {
31+
32+
}
+3-3
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@
2323
import org.springframework.boot.autoconfigure.SpringBootApplication;
2424

2525
@SpringBootApplication
26-
public class SampleXmlApplication implements CommandLineRunner {
26+
public class SampleMybatisApplication implements CommandLineRunner {
2727

2828
public static void main(String[] args) {
29-
SpringApplication.run(SampleXmlApplication.class, args);
29+
SpringApplication.run(SampleMybatisApplication.class, args);
3030
}
3131

3232
private final CityDao cityDao;
3333

3434
private final HotelMapper hotelMapper;
3535

36-
public SampleXmlApplication(CityDao cityDao, HotelMapper hotelMapper) {
36+
public SampleMybatisApplication(CityDao cityDao, HotelMapper hotelMapper) {
3737
this.cityDao = cityDao;
3838
this.hotelMapper = hotelMapper;
3939
}
Original file line numberDiff line numberDiff line change
@@ -14,50 +14,37 @@
1414
* limitations under the License.
1515
*/
1616

17-
package sample.mybatis;
17+
package sample.mybatis.dao;
1818

19-
import org.apache.ibatis.session.SqlSession;
2019
import org.junit.Test;
2120
import org.junit.runner.RunWith;
2221
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
2322
import org.springframework.beans.factory.annotation.Autowired;
24-
import org.springframework.boot.test.context.TestConfiguration;
25-
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Import;
2624
import org.springframework.test.context.junit4.SpringRunner;
27-
import sample.mybatis.dao.CityDao;
2825
import sample.mybatis.domain.City;
2926

3027
import static org.assertj.core.api.Assertions.assertThat;
3128

3229
/**
30+
* Tests for {@link CityDao}.
3331
* @author wonwoo
3432
* @since 1.2.1
3533
*/
3634
@RunWith(SpringRunner.class)
3735
@MybatisTest
38-
public class SampleSqlSessionTest {
36+
@Import(CityDao.class)
37+
public class CityDaoTest {
3938

4039
@Autowired
41-
private SqlSession sqlSession;
42-
43-
@Test
44-
public void sqlSessionIsNotNullTest() {
45-
assertThat(sqlSession).isNotNull();
46-
}
40+
private CityDao cityDao;
4741

4842
@Test
4943
public void selectCityByIdTest() {
50-
City city = sqlSession.selectOne("selectCityById", 1);
44+
City city = cityDao.selectCityById(1);
5145
assertThat(city.getName()).isEqualTo("San Francisco");
5246
assertThat(city.getState()).isEqualTo("CA");
5347
assertThat(city.getCountry()).isEqualTo("US");
5448
}
5549

56-
@TestConfiguration
57-
public static class Config {
58-
@Bean
59-
public CityDao cityDao(SqlSession sqlSession) {
60-
return new CityDao(sqlSession);
61-
}
62-
}
6350
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 sample.mybatis.dao;
17+
18+
import org.springframework.boot.autoconfigure.SpringBootApplication;
19+
20+
/**
21+
* The Spring Boot Application for testing {@link org.mybatis.spring.boot.test.autoconfigure.MybatisTest @MybatisTest}.
22+
* <p>
23+
* This class has role for prevent to run the {@link sample.mybatis.SampleMybatisApplication}.
24+
* For more detail information, please refer <a href="http://stackoverflow.com/questions/42722480/jdbctest-detect-class-annotated-springbootapplication">Here</a>.
25+
*
26+
* @author Kazuki Shimizu
27+
* @since 1.2.1
28+
*/
29+
@SpringBootApplication
30+
public class DaoTestApplication {
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 sample.mybatis.mapper;
17+
18+
import org.junit.Test;
19+
import org.junit.runner.RunWith;
20+
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.test.context.junit4.SpringRunner;
23+
import sample.mybatis.domain.Hotel;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
/**
28+
* Tests for {@link HotelMapper}.
29+
* @author wonwoo
30+
* @since 1.2.1
31+
*/
32+
@RunWith(SpringRunner.class)
33+
@MybatisTest
34+
public class HotelMapperTest {
35+
36+
@Autowired
37+
private HotelMapper hotelMapper;
38+
39+
@Test
40+
public void selectByCityIdTest() {
41+
Hotel hotel = hotelMapper.selectByCityId(1);
42+
assertThat(hotel.getName()).isEqualTo("Conrad Treasury Place");
43+
assertThat(hotel.getAddress()).isEqualTo("William & George Streets");
44+
assertThat(hotel.getZip()).isEqualTo("4001");
45+
}
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 sample.mybatis.mapper;
17+
18+
import org.springframework.boot.autoconfigure.SpringBootApplication;
19+
20+
/**
21+
* The Spring Boot Application for testing {@link org.mybatis.spring.boot.test.autoconfigure.MybatisTest @MybatisTest}.
22+
* <p>
23+
* This class has role for prevent to run the {@link sample.mybatis.SampleMybatisApplication}.
24+
* For more detail information, please refer <a href="http://stackoverflow.com/questions/42722480/jdbctest-detect-class-annotated-springbootapplication">Here</a>.
25+
*
26+
* @author Kazuki Shimizu
27+
* @since 1.2.1
28+
*/
29+
@SpringBootApplication
30+
public class MapperTestApplication {
31+
32+
}

0 commit comments

Comments
 (0)