Do not fail if h2Console bean cannot connect to db

See gh-23566
This commit is contained in:
Shraddha Yeole
2020-10-01 16:24:43 -07:00
committed by Madhura Bhave
parent 3118ca9313
commit 2d76de29ff
2 changed files with 27 additions and 1 deletions

View File

@@ -74,7 +74,7 @@ public class H2ConsoleAutoConfiguration {
logger.info("H2 console available at '" + path + "'. Database available at '"
+ connection.getMetaData().getURL() + "'");
}
catch (SQLException ex) {
catch (Exception ex) {
// Continue
}
});

View File

@@ -17,11 +17,13 @@
package org.springframework.boot.autoconfigure.h2;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.BDDMockito;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanCreationException;
@@ -31,8 +33,11 @@ import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link H2ConsoleAutoConfiguration}
@@ -117,4 +122,25 @@ class H2ConsoleAutoConfigurationTests {
});
}
@Test
void testDataSource() {
this.contextRunner.withUserConfiguration(DataSourceAvailable.class)
.withPropertyValues("spring.h2.console.enabled=true").run((context) -> {
assertThat(context.isRunning()).isTrue();
});
}
@Configuration(proxyBeanMethods = false)
static class DataSourceAvailable {
@Bean
public DataSource dataSource() throws SQLException {
DataSource dataSource = mock(DataSource.class);
BDDMockito.when(dataSource.getConnection()).thenThrow(IllegalStateException.class);
return dataSource;
}
}
}