25
25
import org .junit .jupiter .api .BeforeEach ;
26
26
import org .junit .jupiter .api .Test ;
27
27
28
+ import org .springframework .jdbc .CannotGetJdbcConnectionException ;
29
+
30
+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
28
31
import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
29
32
import static org .mockito .BDDMockito .given ;
30
33
import static org .mockito .Mockito .mock ;
@@ -48,18 +51,28 @@ class DatabaseStartupValidatorTests {
48
51
@ BeforeEach
49
52
void setUp () throws Exception {
50
53
given (dataSource .getConnection ()).willReturn (connection );
54
+ validator .setDataSource (dataSource );
55
+ validator .setTimeout (3 ); // ensure tests don't accidentally run too long
56
+ }
57
+
58
+ @ Test
59
+ void exceededTimeoutThrowsException () {
60
+ validator .setTimeout (1 );
61
+ assertThatExceptionOfType (CannotGetJdbcConnectionException .class )
62
+ .isThrownBy (validator ::afterPropertiesSet );
51
63
}
52
64
53
65
@ Test
54
66
void properSetupForDataSource () {
67
+ validator .setDataSource (null );
68
+
55
69
assertThatIllegalArgumentException ().isThrownBy (validator ::afterPropertiesSet );
56
70
}
57
71
58
72
@ Test
59
73
void shouldUseJdbc4IsValidByDefault () throws Exception {
60
74
given (connection .isValid (1 )).willReturn (true );
61
75
62
- validator .setDataSource (dataSource );
63
76
validator .afterPropertiesSet ();
64
77
65
78
verify (connection , times (1 )).isValid (1 );
@@ -70,7 +83,6 @@ void shouldUseJdbc4IsValidByDefault() throws Exception {
70
83
void shouldCallValidatonTwiceWhenNotValid () throws Exception {
71
84
given (connection .isValid (1 )).willReturn (false , true );
72
85
73
- validator .setDataSource (dataSource );
74
86
validator .afterPropertiesSet ();
75
87
76
88
verify (connection , times (2 )).isValid (1 );
@@ -81,7 +93,6 @@ void shouldCallValidatonTwiceWhenNotValid() throws Exception {
81
93
void shouldCallValidatonTwiceInCaseOfException () throws Exception {
82
94
given (connection .isValid (1 )).willThrow (new SQLException ("Test" )).willReturn (true );
83
95
84
- validator .setDataSource (dataSource );
85
96
validator .afterPropertiesSet ();
86
97
87
98
verify (connection , times (2 )).isValid (1 );
@@ -96,7 +107,6 @@ void useValidationQueryInsteadOfIsValid() throws Exception {
96
107
given (connection .createStatement ()).willReturn (statement );
97
108
given (statement .execute (validationQuery )).willReturn (true );
98
109
99
- validator .setDataSource (dataSource );
100
110
validator .setValidationQuery (validationQuery );
101
111
validator .afterPropertiesSet ();
102
112
@@ -116,7 +126,6 @@ void shouldExecuteValidatonTwiceOnError() throws Exception {
116
126
.willThrow (new SQLException ("Test" ))
117
127
.willReturn (true );
118
128
119
- validator .setDataSource (dataSource );
120
129
validator .setValidationQuery (validationQuery );
121
130
validator .afterPropertiesSet ();
122
131
0 commit comments