25
25
import org .junit .jupiter .api .BeforeEach ;
26
26
import org .junit .jupiter .api .Test ;
27
27
28
- import static org .assertj .core .api .Assertions .assertThatThrownBy ;
28
+ import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
29
29
import static org .mockito .BDDMockito .given ;
30
30
import static org .mockito .Mockito .mock ;
31
31
import static org .mockito .Mockito .times ;
32
32
import static org .mockito .Mockito .verify ;
33
33
34
34
/**
35
- * Mock object based test for {@code DatabaseStartupValidator}.
35
+ * Mock object based tests for {@code DatabaseStartupValidator}.
36
36
*
37
- * @author Marten Deinum,
37
+ * @author Marten Deinum
38
38
*/
39
39
class DatabaseStartupValidatorTests {
40
40
41
- private Connection connection ;
42
- private DataSource dataSource ;
41
+ private final DataSource dataSource = mock (DataSource .class );
42
+
43
+ private final Connection connection = mock (Connection .class );
44
+
45
+ private final DatabaseStartupValidator validator = new DatabaseStartupValidator ();
46
+
43
47
44
48
@ BeforeEach
45
- public void setUp () throws Exception {
46
- connection = mock (Connection .class );
47
- dataSource = mock (DataSource .class );
49
+ void setUp () throws Exception {
48
50
given (dataSource .getConnection ()).willReturn (connection );
51
+ validator .setDataSource (dataSource );
49
52
}
50
53
51
54
@ Test
52
- public void properSetupForDataSource () {
53
- DatabaseStartupValidator validator = new DatabaseStartupValidator ();
54
- assertThatThrownBy (validator ::afterPropertiesSet )
55
- .isInstanceOf (IllegalArgumentException .class );
55
+ void properSetupForDataSource () {
56
+ assertThatIllegalArgumentException ().isThrownBy (validator ::afterPropertiesSet );
56
57
}
57
58
58
59
@ Test
59
- public void shouldUseJdbc4IsValidByDefault () throws Exception {
60
+ void shouldUseJdbc4IsValidByDefault () throws Exception {
60
61
given (connection .isValid (1 )).willReturn (true );
61
- DatabaseStartupValidator validator = new DatabaseStartupValidator ();
62
- validator .setDataSource (dataSource );
62
+
63
63
validator .afterPropertiesSet ();
64
64
65
65
verify (connection , times (1 )).isValid (1 );
66
66
verify (connection , times (1 )).close ();
67
-
68
67
}
69
68
70
69
@ Test
71
- public void shouldCallValidatonTwiceWhenNotValid () throws Exception {
70
+ void shouldCallValidatonTwiceWhenNotValid () throws Exception {
72
71
given (connection .isValid (1 )).willReturn (false , true );
73
- DatabaseStartupValidator validator = new DatabaseStartupValidator ();
74
- validator .setDataSource (dataSource );
72
+
75
73
validator .afterPropertiesSet ();
76
74
77
75
verify (connection , times (2 )).isValid (1 );
78
76
verify (connection , times (2 )).close ();
79
-
80
77
}
81
78
82
79
@ Test
83
- public void shouldCallValidatonTwiceInCaseOfException () throws Exception {
80
+ void shouldCallValidatonTwiceInCaseOfException () throws Exception {
84
81
given (connection .isValid (1 )).willThrow (new SQLException ("Test" )).willReturn (true );
85
- DatabaseStartupValidator validator = new DatabaseStartupValidator ();
86
- validator .setDataSource (dataSource );
82
+
87
83
validator .afterPropertiesSet ();
88
84
89
85
verify (connection , times (2 )).isValid (1 );
90
86
verify (connection , times (2 )).close ();
91
-
92
87
}
93
88
94
89
@ Test
95
- public void useValidationQueryInsteadOfIsValid () throws Exception {
90
+ @ SuppressWarnings ("deprecation" )
91
+ void useValidationQueryInsteadOfIsValid () throws Exception {
96
92
String validationQuery = "SELECT NOW() FROM DUAL" ;
97
93
Statement statement = mock (Statement .class );
98
94
given (connection .createStatement ()).willReturn (statement );
99
95
given (statement .execute (validationQuery )).willReturn (true );
100
96
101
- DatabaseStartupValidator validator = new DatabaseStartupValidator ();
102
- validator .setDataSource (dataSource );
103
97
validator .setValidationQuery (validationQuery );
104
98
validator .afterPropertiesSet ();
105
99
@@ -110,16 +104,15 @@ public void useValidationQueryInsteadOfIsValid() throws Exception {
110
104
}
111
105
112
106
@ Test
113
- public void shouldExecuteValidatonTwiceOnError () throws Exception {
107
+ @ SuppressWarnings ("deprecation" )
108
+ void shouldExecuteValidatonTwiceOnError () throws Exception {
114
109
String validationQuery = "SELECT NOW() FROM DUAL" ;
115
110
Statement statement = mock (Statement .class );
116
111
given (connection .createStatement ()).willReturn (statement );
117
112
given (statement .execute (validationQuery ))
118
113
.willThrow (new SQLException ("Test" ))
119
114
.willReturn (true );
120
115
121
- DatabaseStartupValidator validator = new DatabaseStartupValidator ();
122
- validator .setDataSource (dataSource );
123
116
validator .setValidationQuery (validationQuery );
124
117
validator .afterPropertiesSet ();
125
118
@@ -128,4 +121,5 @@ public void shouldExecuteValidatonTwiceOnError() throws Exception {
128
121
verify (connection , times (2 )).close ();
129
122
verify (statement , times (2 )).close ();
130
123
}
124
+
131
125
}
0 commit comments