1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.no_result_type_map;
17
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19 import static org.junit.jupiter.api.Assertions.assertThrows;
20
21 import java.io.Reader;
22 import java.util.List;
23
24 import org.apache.ibatis.BaseDataTest;
25 import org.apache.ibatis.exceptions.PersistenceException;
26 import org.apache.ibatis.executor.ExecutorException;
27 import org.apache.ibatis.io.Resources;
28 import org.apache.ibatis.session.SqlSession;
29 import org.apache.ibatis.session.SqlSessionFactory;
30 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
31 import org.junit.jupiter.api.Assertions;
32 import org.junit.jupiter.api.BeforeAll;
33 import org.junit.jupiter.api.Test;
34
35 class NoResultTypeMapTest {
36
37 private static SqlSessionFactory sqlSessionFactory;
38
39 @BeforeAll
40 static void setUp() throws Exception {
41
42 try (Reader reader = Resources
43 .getResourceAsReader("org/apache/ibatis/submitted/no_result_type_map/mybatis-config.xml")) {
44 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
45 sqlSessionFactory.getConfiguration().addMapper(Mapper.class);
46 }
47
48
49 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
50 "org/apache/ibatis/submitted/no_result_type_map/CreateDB.sql");
51 }
52
53 @Test
54 void shouldGetAUser() {
55 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
56 Mapper mapper = sqlSession.getMapper(Mapper.class);
57 User user = mapper.getUser(1);
58 Assertions.assertEquals("User1", user.getName());
59 }
60 }
61
62 @Test
63 void shouldGetAllUsers() {
64 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
65 Mapper mapper = sqlSession.getMapper(Mapper.class);
66 List<User> users = mapper.getAllUsers();
67 Assertions.assertEquals(3, users.size());
68 }
69 }
70
71 @Test
72 void shouldResolveInheritedReturnType() {
73 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
74 Mapper mapper = sqlSession.getMapper(Mapper.class);
75 List<User> users = mapper.getAllUsersInParent();
76 Assertions.assertEquals(3, users.size());
77 }
78 }
79
80 @Test
81 void shouldFailIfNoMatchingMethod() {
82 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
83 PersistenceException ex = assertThrows(PersistenceException.class,
84 () -> sqlSession.selectList("org.apache.ibatis.submitted.no_result_type_map.Mapper.noMatchingMethod"));
85 ExecutorException cause = (ExecutorException) ex.getCause();
86 assertEquals(
87 "A query was run and no Result Maps were found for the Mapped Statement "
88 + "'org.apache.ibatis.submitted.no_result_type_map.Mapper.noMatchingMethod'. "
89 + "'resultType' or 'resultMap' must be specified when there is no corresponding method.",
90 cause.getMessage());
91 }
92 }
93
94 }