Skip to content

Commit c0f158d

Browse files
committed
Polish "Do not fail if h2Console bean cannot connect to db"
See gh-23566
1 parent 2d76de2 commit c0f158d

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.boot.autoconfigure.h2;
1818

1919
import java.sql.Connection;
20-
import java.sql.SQLException;
2120

2221
import javax.sql.DataSource;
2322

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import org.junit.jupiter.api.Test;
2525
import org.junit.jupiter.api.extension.ExtendWith;
26-
import org.mockito.BDDMockito;
2726

2827
import org.springframework.beans.BeansException;
2928
import org.springframework.beans.factory.BeanCreationException;
@@ -37,6 +36,7 @@
3736
import org.springframework.context.annotation.Configuration;
3837

3938
import static org.assertj.core.api.Assertions.assertThat;
39+
import static org.mockito.BDDMockito.given;
4040
import static org.mockito.Mockito.mock;
4141

4242
/**
@@ -45,6 +45,7 @@
4545
* @author Andy Wilkinson
4646
* @author Marten Deinum
4747
* @author Stephane Nicoll
48+
* @author Shraddha Yeole
4849
*/
4950
class H2ConsoleAutoConfigurationTests {
5051

@@ -123,24 +124,22 @@ void dataSourceUrlIsLoggedWhenAvailable(CapturedOutput output) throws BeansExcep
123124
}
124125

125126
@Test
126-
void testDataSource() {
127-
this.contextRunner.withUserConfiguration(DataSourceAvailable.class)
128-
.withPropertyValues("spring.h2.console.enabled=true").run((context) -> {
129-
assertThat(context.isRunning()).isTrue();
130-
});
127+
void h2ConsoleShouldNotFailIfDatabaseConnectionFails() {
128+
this.contextRunner.withUserConfiguration(CustomDataSourceConfiguration.class)
129+
.withPropertyValues("spring.h2.console.enabled=true")
130+
.run((context) -> assertThat(context.isRunning()).isTrue());
131131
}
132132

133133
@Configuration(proxyBeanMethods = false)
134-
static class DataSourceAvailable {
134+
static class CustomDataSourceConfiguration {
135135

136136
@Bean
137-
public DataSource dataSource() throws SQLException {
137+
DataSource dataSource() throws SQLException {
138138
DataSource dataSource = mock(DataSource.class);
139-
BDDMockito.when(dataSource.getConnection()).thenThrow(IllegalStateException.class);
139+
given(dataSource.getConnection()).willThrow(IllegalStateException.class);
140140
return dataSource;
141141
}
142142

143143
}
144144

145-
146145
}

0 commit comments

Comments
 (0)