Skip to content

Commit 2d76de2

Browse files
Shraddha-Yeolembhave
authored andcommitted
Do not fail if h2Console bean cannot connect to db
See gh-23566
1 parent 3118ca9 commit 2d76de2

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public ServletRegistrationBean<WebServlet> h2Console(H2ConsoleProperties propert
7474
logger.info("H2 console available at '" + path + "'. Database available at '"
7575
+ connection.getMetaData().getURL() + "'");
7676
}
77-
catch (SQLException ex) {
77+
catch (Exception ex) {
7878
// Continue
7979
}
8080
});

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
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

2324
import org.junit.jupiter.api.Test;
2425
import org.junit.jupiter.api.extension.ExtendWith;
26+
import org.mockito.BDDMockito;
2527

2628
import org.springframework.beans.BeansException;
2729
import org.springframework.beans.factory.BeanCreationException;
@@ -31,8 +33,11 @@
3133
import org.springframework.boot.test.system.CapturedOutput;
3234
import org.springframework.boot.test.system.OutputCaptureExtension;
3335
import org.springframework.boot.web.servlet.ServletRegistrationBean;
36+
import org.springframework.context.annotation.Bean;
37+
import org.springframework.context.annotation.Configuration;
3438

3539
import static org.assertj.core.api.Assertions.assertThat;
40+
import static org.mockito.Mockito.mock;
3641

3742
/**
3843
* Tests for {@link H2ConsoleAutoConfiguration}
@@ -117,4 +122,25 @@ void dataSourceUrlIsLoggedWhenAvailable(CapturedOutput output) throws BeansExcep
117122
});
118123
}
119124

125+
@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+
});
131+
}
132+
133+
@Configuration(proxyBeanMethods = false)
134+
static class DataSourceAvailable {
135+
136+
@Bean
137+
public DataSource dataSource() throws SQLException {
138+
DataSource dataSource = mock(DataSource.class);
139+
BDDMockito.when(dataSource.getConnection()).thenThrow(IllegalStateException.class);
140+
return dataSource;
141+
}
142+
143+
}
144+
145+
120146
}

0 commit comments

Comments
 (0)