Skip to content

Commit 7517394

Browse files
committed
Polishing
1 parent 51cc719 commit 7517394

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

spring-jdbc/src/test/java/org/springframework/jdbc/support/DatabaseStartupValidatorTests.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
import org.junit.jupiter.api.BeforeEach;
2626
import org.junit.jupiter.api.Test;
2727

28+
import org.springframework.jdbc.CannotGetJdbcConnectionException;
29+
30+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2831
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
2932
import static org.mockito.BDDMockito.given;
3033
import static org.mockito.Mockito.mock;
@@ -48,18 +51,28 @@ class DatabaseStartupValidatorTests {
4851
@BeforeEach
4952
void setUp() throws Exception {
5053
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);
5163
}
5264

5365
@Test
5466
void properSetupForDataSource() {
67+
validator.setDataSource(null);
68+
5569
assertThatIllegalArgumentException().isThrownBy(validator::afterPropertiesSet);
5670
}
5771

5872
@Test
5973
void shouldUseJdbc4IsValidByDefault() throws Exception {
6074
given(connection.isValid(1)).willReturn(true);
6175

62-
validator.setDataSource(dataSource);
6376
validator.afterPropertiesSet();
6477

6578
verify(connection, times(1)).isValid(1);
@@ -70,7 +83,6 @@ void shouldUseJdbc4IsValidByDefault() throws Exception {
7083
void shouldCallValidatonTwiceWhenNotValid() throws Exception {
7184
given(connection.isValid(1)).willReturn(false, true);
7285

73-
validator.setDataSource(dataSource);
7486
validator.afterPropertiesSet();
7587

7688
verify(connection, times(2)).isValid(1);
@@ -81,7 +93,6 @@ void shouldCallValidatonTwiceWhenNotValid() throws Exception {
8193
void shouldCallValidatonTwiceInCaseOfException() throws Exception {
8294
given(connection.isValid(1)).willThrow(new SQLException("Test")).willReturn(true);
8395

84-
validator.setDataSource(dataSource);
8596
validator.afterPropertiesSet();
8697

8798
verify(connection, times(2)).isValid(1);
@@ -96,7 +107,6 @@ void useValidationQueryInsteadOfIsValid() throws Exception {
96107
given(connection.createStatement()).willReturn(statement);
97108
given(statement.execute(validationQuery)).willReturn(true);
98109

99-
validator.setDataSource(dataSource);
100110
validator.setValidationQuery(validationQuery);
101111
validator.afterPropertiesSet();
102112

@@ -116,7 +126,6 @@ void shouldExecuteValidatonTwiceOnError() throws Exception {
116126
.willThrow(new SQLException("Test"))
117127
.willReturn(true);
118128

119-
validator.setDataSource(dataSource);
120129
validator.setValidationQuery(validationQuery);
121130
validator.afterPropertiesSet();
122131

0 commit comments

Comments
 (0)