1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.mapper_extend;
17
18 import static com.googlecode.catchexception.apis.BDDCatchException.*;
19 import static org.assertj.core.api.BDDAssertions.then;
20
21 import java.io.Reader;
22
23 import org.apache.ibatis.BaseDataTest;
24 import org.apache.ibatis.binding.BindingException;
25 import org.apache.ibatis.io.Resources;
26 import org.apache.ibatis.session.SqlSession;
27 import org.apache.ibatis.session.SqlSessionFactory;
28 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
29 import org.junit.jupiter.api.Assertions;
30 import org.junit.jupiter.api.BeforeAll;
31 import org.junit.jupiter.api.Test;
32
33 class MapperExtendTest {
34
35 private static SqlSessionFactory sqlSessionFactory;
36
37 @BeforeAll
38 static void setUp() throws Exception {
39
40 try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/mapper_extend/mybatis-config.xml")) {
41 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
42 }
43
44
45 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
46 "org/apache/ibatis/submitted/mapper_extend/CreateDB.sql");
47 }
48
49 @Test
50 void shouldGetAUserWithAnExtendedXMLMethod() {
51 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
52 ParentMapper mapper = sqlSession.getMapper(Mapper.class);
53 User user = mapper.getUserXML();
54 Assertions.assertEquals("User1", user.getName());
55 }
56 }
57
58 @Test
59 void shouldGetAUserWithAnExtendedAnnotatedMethod() {
60 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
61 ParentMapper mapper = sqlSession.getMapper(Mapper.class);
62 User user = mapper.getUserAnnotated();
63 Assertions.assertEquals("User1", user.getName());
64 }
65 }
66
67 @Test
68 void shouldGetAUserWithAnOverloadedXMLMethod() {
69 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
70 ParentMapper mapper = sqlSession.getMapper(MapperOverload.class);
71 User user = mapper.getUserXML();
72 Assertions.assertEquals("User2", user.getName());
73 }
74 }
75
76 @Test
77 void shouldGetAUserWithAnOverloadedAnnotatedMethod() {
78 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
79 ParentMapper mapper = sqlSession.getMapper(MapperOverload.class);
80 User user = mapper.getUserAnnotated();
81 Assertions.assertEquals("User2", user.getName());
82 }
83 }
84
85 @Test
86 void shouldFindStatementInSubInterfaceOfDeclaringClass() {
87 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
88 ChildMapper mapper = sqlSession.getMapper(ChildMapper.class);
89 User user = mapper.getUserByName("User1");
90 Assertions.assertNotNull(user);
91 }
92 }
93
94 @Test
95 void shouldThrowExceptionIfNoMatchingStatementFound() {
96 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
97 Mapper mapper = sqlSession.getMapper(Mapper.class);
98 when(mapper::noMappedStatement);
99 then(caughtException()).isInstanceOf(BindingException.class)
100 .hasMessage("Invalid bound statement (not found): "
101 + Mapper.class.getName() + ".noMappedStatement");
102 }
103 }
104 }