1
+ package com .baeldung .spring .jdbc .queryforlist ;
2
+
3
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
5
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
6
+
7
+ import java .util .List ;
8
+ import java .util .Map ;
9
+
10
+ import org .junit .jupiter .api .Test ;
11
+ import org .springframework .beans .factory .annotation .Autowired ;
12
+ import org .springframework .boot .test .autoconfigure .jdbc .JdbcTest ;
13
+ import org .springframework .jdbc .IncorrectResultSetColumnCountException ;
14
+ import org .springframework .jdbc .core .BeanPropertyRowMapper ;
15
+ import org .springframework .jdbc .core .JdbcTemplate ;
16
+ import org .springframework .test .context .jdbc .Sql ;
17
+
18
+ @ JdbcTest
19
+ @ Sql (value = { "init-student-data.sql" }, executionPhase = Sql .ExecutionPhase .BEFORE_TEST_CLASS )
20
+ class JdbcTemplateIntegrationTest {
21
+
22
+ @ Autowired
23
+ private JdbcTemplate jdbcTemplate ;
24
+
25
+ @ Test
26
+ void whenUsingQueryForListToGetStudentList_thenGotException () {
27
+ assertThrows (IncorrectResultSetColumnCountException .class , () -> jdbcTemplate .queryForList ("SELECT * FROM STUDENT_TBL" , Student .class ));
28
+ }
29
+
30
+ @ Test
31
+ void whenUsingQueryForListToSingleColumn_thenCorrect () {
32
+ List <String > names = jdbcTemplate .queryForList ("SELECT NAME FROM STUDENT_TBL" , String .class );
33
+ assertEquals (List .of ("Kai" , "Eric" , "Kevin" , "Liam" ), names );
34
+
35
+ List <Integer > ids = jdbcTemplate .queryForList ("SELECT ID FROM STUDENT_TBL" , Integer .class );
36
+ assertEquals (List .of (1 , 2 , 3 , 4 ), ids );
37
+ }
38
+
39
+ @ Test
40
+ void whenUsingQueryForListToGetMap_thenCorrect () {
41
+ List <Map <String , Object >> nameMajorRowMaps = jdbcTemplate .queryForList ("SELECT NAME, MAJOR FROM STUDENT_TBL" );
42
+
43
+ // @formatter:off
44
+ assertEquals (List .of (
45
+ Map .of ("NAME" , "Kai" , "MAJOR" , "Computer Science" ),
46
+ Map .of ("NAME" , "Eric" , "MAJOR" , "Computer Science" ),
47
+ Map .of ("NAME" , "Kevin" , "MAJOR" , "Banking" ),
48
+ Map .of ("NAME" , "Liam" , "MAJOR" , "Law" )
49
+ ), nameMajorRowMaps );
50
+ // @formatter:on
51
+
52
+ List <Map <String , Object >> rowMaps = jdbcTemplate .queryForList ("SELECT * FROM STUDENT_TBL" );
53
+
54
+ // @formatter:off
55
+ assertEquals (List .of (
56
+ Map .of ("ID" , 1 , "NAME" , "Kai" , "MAJOR" , "Computer Science" ),
57
+ Map .of ("ID" , 2 , "NAME" , "Eric" , "MAJOR" , "Computer Science" ),
58
+ Map .of ("ID" , 3 , "NAME" , "Kevin" , "MAJOR" , "Banking" ),
59
+ Map .of ("ID" , 4 , "NAME" , "Liam" , "MAJOR" , "Law" )
60
+ ), rowMaps );
61
+ // @formatter:on
62
+ }
63
+
64
+ @ Test
65
+ void whenUsingQueryWithRowMapperToGetStudentList_thenCorrect () {
66
+ // @formatter:off
67
+ List <Student > expected = List .of (
68
+ new Student (1 , "Kai" , "Computer Science" ),
69
+ new Student (2 , "Eric" , "Computer Science" ),
70
+ new Student (3 , "Kevin" , "Banking" ),
71
+ new Student (4 , "Liam" , "Law" )
72
+ );
73
+ // @formatter:on
74
+
75
+ List <Student > students = jdbcTemplate .query ("SELECT * FROM STUDENT_TBL" , new BeanPropertyRowMapper <>(Student .class ));
76
+
77
+ assertEquals (expected , students );
78
+ }
79
+
80
+ }
0 commit comments