11package cn .mybatisboost .test ;
22
3+ import cn .mybatisboost .core .GenericMapper ;
34import org .junit .After ;
45import org .junit .Before ;
56import org .junit .Test ;
67import org .junit .runner .RunWith ;
78import org .springframework .beans .factory .annotation .Autowired ;
9+ import org .springframework .beans .factory .annotation .Qualifier ;
10+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
11+ import org .springframework .boot .test .context .SpringBootTest ;
812import org .springframework .jdbc .core .JdbcTemplate ;
913import org .springframework .test .context .junit4 .SpringRunner ;
1014
15+ import java .lang .reflect .Method ;
1116import java .util .List ;
1217
1318import static org .junit .Assert .assertEquals ;
1419
20+ /**
21+ * We use reflection to test Nosql feature
22+ * mainly because we mustn't but have to load mapper classes before there were scanned by Mybatis
23+ */
1524@ RunWith (SpringRunner .class )
25+ @ SpringBootApplication
26+ @ SpringBootTest (classes = GenericMapper .class )
27+ @ SuppressWarnings ("unchecked" )
1628public class NosqlTest {
1729
1830 @ Autowired
19- private ProjectNosqlMapper mapper ;
31+ @ Qualifier ("projectNosqlMapper" )
32+ private GenericMapper mapper ;
2033 @ Autowired
2134 private JdbcTemplate jdbcTemplate ;
2235
@@ -33,55 +46,63 @@ public void tearDown() {
3346 }
3447
3548 @ Test
36- public void deleteAll () {
37- assertEquals (3 , mapper .deleteAll ());
49+ public void deleteAll () throws Exception {
50+ Method deleteAll = mapper .getClass ().getDeclaredMethod ("deleteAll" );
51+ assertEquals (3 , deleteAll .invoke (mapper ));
3852 jdbcTemplate .query ("select * from project" , resultSet -> {
3953 assertEquals (0 , resultSet .getRow ());
4054 });
4155 }
4256
4357 @ Test
44- public void selectFirst () {
45- assertEquals (123 , (int ) mapper .selectFirst ().getId ());
58+ public void selectFirst () throws Exception {
59+ Method selectFirst = mapper .getClass ().getDeclaredMethod ("selectFirst" );
60+ assertEquals (123 , (int ) ((Project ) selectFirst .invoke (mapper )).getId ());
4661 }
4762
4863 @ Test
49- public void selectTop2 () {
50- List <Project > list = mapper .selectTop2 ();
64+ public void selectTop2 () throws Exception {
65+ Method selectTop2 = mapper .getClass ().getDeclaredMethod ("selectTop2" );
66+ List <Project > list = (List <Project >) selectTop2 .invoke (mapper );
5167 assertEquals (2 , list .size ());
5268 assertEquals (123 , (int ) list .get (0 ).getId ());
5369 assertEquals (456 , (int ) list .get (1 ).getId ());
5470 }
5571
5672 @ Test
57- public void selectAllOffset1Limit1 () {
58- assertEquals (456 , (int ) mapper .selectAllOffset1Limit1 ().getId ());
73+ public void selectAllOffset1Limit1 () throws Exception {
74+ Method selectAllOffset1Limit1 = mapper .getClass ().getDeclaredMethod ("selectAllOffset1Limit1" );
75+ assertEquals (456 , (int ) ((Project ) selectAllOffset1Limit1 .invoke (mapper )).getId ());
5976 }
6077
6178 @ Test
62- public void selectByGroupIdAndArtifactId () {
63- assertEquals (123 , (int ) mapper .selectByGroupIdAndArtifactId ("cn.mybatisboost1" , "mybatis-boost1" ).get (0 ).getId ());
79+ public void selectByGroupIdAndArtifactId () throws Exception {
80+ Method selectByGroupIdAndArtifactId = mapper .getClass ().getDeclaredMethod ("selectByGroupIdAndArtifactId" , String .class , String .class );
81+ assertEquals (123 , (int ) ((Project ) selectByGroupIdAndArtifactId .invoke (mapper , "cn.mybatisboost1" , "mybatis-boost1" )).getId ());
6482 }
6583
6684 @ Test
67- public void selectByGroupIdOrArtifactId () {
68- List <Project > list = mapper .selectByGroupIdOrArtifactId ("cn.mybatisboost1" , "mybatis-boost2" );
85+ public void selectByGroupIdOrArtifactId () throws Exception {
86+ Method selectByGroupIdOrArtifactId = mapper .getClass ().getDeclaredMethod ("selectByGroupIdOrArtifactId" , String .class , String .class );
87+ List <Project > list = (List <Project >) selectByGroupIdOrArtifactId .invoke (mapper , "cn.mybatisboost1" , "mybatis-boost2" );
6988 assertEquals (2 , list .size ());
7089 assertEquals (123 , (int ) list .get (0 ).getId ());
7190 assertEquals (456 , (int ) list .get (1 ).getId ());
7291 }
7392
7493 @ Test
75- public void selectByNotArtifactId () {
76- List <Project > list = mapper .selectByArtifactIdNot ("mybatis-boost1" );
94+ public void selectByNotArtifactId () throws Exception {
95+ Method selectByArtifactIdNot = mapper .getClass ().getDeclaredMethod ("selectByArtifactIdNot" , String .class );
96+ List <Project > list = (List <Project >) selectByArtifactIdNot .invoke (mapper , "mybatis-boost1" );
7797 assertEquals (2 , list .size ());
7898 assertEquals (456 , (int ) list .get (0 ).getId ());
7999 assertEquals (789 , (int ) list .get (1 ).getId ());
80100 }
81101
82102 @ Test
83- public void selectAllOrderByGroupIdDesc () {
84- List <Project > list = mapper .selectAllOrderByGroupIdDesc ();
103+ public void selectAllOrderByGroupIdDesc () throws Exception {
104+ Method selectAllOrderByGroupIdDesc = mapper .getClass ().getDeclaredMethod ("selectAllOrderByGroupIdDesc" );
105+ List <Project > list = (List <Project >) selectAllOrderByGroupIdDesc .invoke (mapper );
85106 assertEquals (3 , list .size ());
86107 assertEquals (789 , (int ) list .get (0 ).getId ());
87108 assertEquals (456 , (int ) list .get (1 ).getId ());
0 commit comments