Skip to content

Commit 1edc08f

Browse files
committed
Polishing
1 parent f9331d9 commit 1edc08f

File tree

1 file changed

+24
-30
lines changed

1 file changed

+24
-30
lines changed

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

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,81 +25,75 @@
2525
import org.junit.jupiter.api.BeforeEach;
2626
import org.junit.jupiter.api.Test;
2727

28-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
28+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
2929
import static org.mockito.BDDMockito.given;
3030
import static org.mockito.Mockito.mock;
3131
import static org.mockito.Mockito.times;
3232
import static org.mockito.Mockito.verify;
3333

3434
/**
35-
* Mock object based test for {@code DatabaseStartupValidator}.
35+
* Mock object based tests for {@code DatabaseStartupValidator}.
3636
*
37-
* @author Marten Deinum,
37+
* @author Marten Deinum
3838
*/
3939
class DatabaseStartupValidatorTests {
4040

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+
4347

4448
@BeforeEach
45-
public void setUp() throws Exception {
46-
connection = mock(Connection.class);
47-
dataSource = mock(DataSource.class);
49+
void setUp() throws Exception {
4850
given(dataSource.getConnection()).willReturn(connection);
51+
validator.setDataSource(dataSource);
4952
}
5053

5154
@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);
5657
}
5758

5859
@Test
59-
public void shouldUseJdbc4IsValidByDefault() throws Exception {
60+
void shouldUseJdbc4IsValidByDefault() throws Exception {
6061
given(connection.isValid(1)).willReturn(true);
61-
DatabaseStartupValidator validator = new DatabaseStartupValidator();
62-
validator.setDataSource(dataSource);
62+
6363
validator.afterPropertiesSet();
6464

6565
verify(connection, times(1)).isValid(1);
6666
verify(connection, times(1)).close();
67-
6867
}
6968

7069
@Test
71-
public void shouldCallValidatonTwiceWhenNotValid() throws Exception {
70+
void shouldCallValidatonTwiceWhenNotValid() throws Exception {
7271
given(connection.isValid(1)).willReturn(false, true);
73-
DatabaseStartupValidator validator = new DatabaseStartupValidator();
74-
validator.setDataSource(dataSource);
72+
7573
validator.afterPropertiesSet();
7674

7775
verify(connection, times(2)).isValid(1);
7876
verify(connection, times(2)).close();
79-
8077
}
8178

8279
@Test
83-
public void shouldCallValidatonTwiceInCaseOfException() throws Exception {
80+
void shouldCallValidatonTwiceInCaseOfException() throws Exception {
8481
given(connection.isValid(1)).willThrow(new SQLException("Test")).willReturn(true);
85-
DatabaseStartupValidator validator = new DatabaseStartupValidator();
86-
validator.setDataSource(dataSource);
82+
8783
validator.afterPropertiesSet();
8884

8985
verify(connection, times(2)).isValid(1);
9086
verify(connection, times(2)).close();
91-
9287
}
9388

9489
@Test
95-
public void useValidationQueryInsteadOfIsValid() throws Exception {
90+
@SuppressWarnings("deprecation")
91+
void useValidationQueryInsteadOfIsValid() throws Exception {
9692
String validationQuery = "SELECT NOW() FROM DUAL";
9793
Statement statement = mock(Statement.class);
9894
given(connection.createStatement()).willReturn(statement);
9995
given(statement.execute(validationQuery)).willReturn(true);
10096

101-
DatabaseStartupValidator validator = new DatabaseStartupValidator();
102-
validator.setDataSource(dataSource);
10397
validator.setValidationQuery(validationQuery);
10498
validator.afterPropertiesSet();
10599

@@ -110,16 +104,15 @@ public void useValidationQueryInsteadOfIsValid() throws Exception {
110104
}
111105

112106
@Test
113-
public void shouldExecuteValidatonTwiceOnError() throws Exception {
107+
@SuppressWarnings("deprecation")
108+
void shouldExecuteValidatonTwiceOnError() throws Exception {
114109
String validationQuery = "SELECT NOW() FROM DUAL";
115110
Statement statement = mock(Statement.class);
116111
given(connection.createStatement()).willReturn(statement);
117112
given(statement.execute(validationQuery))
118113
.willThrow(new SQLException("Test"))
119114
.willReturn(true);
120115

121-
DatabaseStartupValidator validator = new DatabaseStartupValidator();
122-
validator.setDataSource(dataSource);
123116
validator.setValidationQuery(validationQuery);
124117
validator.afterPropertiesSet();
125118

@@ -128,4 +121,5 @@ public void shouldExecuteValidatonTwiceOnError() throws Exception {
128121
verify(connection, times(2)).close();
129122
verify(statement, times(2)).close();
130123
}
124+
131125
}

0 commit comments

Comments
 (0)