Skip to content

Commit 4dad8c2

Browse files
committed
Merge pull request #23566 from Shraddha-Yeole
* pr/23566: Polish "Do not fail if h2Console bean cannot connect to db" Do not fail if h2Console bean cannot connect to db Closes gh-23566
2 parents 3118ca9 + c0f158d commit 4dad8c2

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

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

Lines changed: 1 addition & 2 deletions
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

@@ -74,7 +73,7 @@ public ServletRegistrationBean<WebServlet> h2Console(H2ConsoleProperties propert
7473
logger.info("H2 console available at '" + path + "'. Database available at '"
7574
+ connection.getMetaData().getURL() + "'");
7675
}
77-
catch (SQLException ex) {
76+
catch (Exception ex) {
7877
// Continue
7978
}
8079
});

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

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

1919
import java.sql.Connection;
20+
import java.sql.SQLException;
2021

2122
import javax.sql.DataSource;
2223

@@ -31,15 +32,20 @@
3132
import org.springframework.boot.test.system.CapturedOutput;
3233
import org.springframework.boot.test.system.OutputCaptureExtension;
3334
import org.springframework.boot.web.servlet.ServletRegistrationBean;
35+
import org.springframework.context.annotation.Bean;
36+
import org.springframework.context.annotation.Configuration;
3437

3538
import static org.assertj.core.api.Assertions.assertThat;
39+
import static org.mockito.BDDMockito.given;
40+
import static org.mockito.Mockito.mock;
3641

3742
/**
3843
* Tests for {@link H2ConsoleAutoConfiguration}
3944
*
4045
* @author Andy Wilkinson
4146
* @author Marten Deinum
4247
* @author Stephane Nicoll
48+
* @author Shraddha Yeole
4349
*/
4450
class H2ConsoleAutoConfigurationTests {
4551

@@ -117,4 +123,23 @@ void dataSourceUrlIsLoggedWhenAvailable(CapturedOutput output) throws BeansExcep
117123
});
118124
}
119125

126+
@Test
127+
void h2ConsoleShouldNotFailIfDatabaseConnectionFails() {
128+
this.contextRunner.withUserConfiguration(CustomDataSourceConfiguration.class)
129+
.withPropertyValues("spring.h2.console.enabled=true")
130+
.run((context) -> assertThat(context.isRunning()).isTrue());
131+
}
132+
133+
@Configuration(proxyBeanMethods = false)
134+
static class CustomDataSourceConfiguration {
135+
136+
@Bean
137+
DataSource dataSource() throws SQLException {
138+
DataSource dataSource = mock(DataSource.class);
139+
given(dataSource.getConnection()).willThrow(IllegalStateException.class);
140+
return dataSource;
141+
}
142+
143+
}
144+
120145
}

0 commit comments

Comments
 (0)