Do not fail if h2Console bean cannot connect to db

See gh-23566
pull/23600/head
Shraddha Yeole 4 years ago committed by Madhura Bhave
parent 3118ca9313
commit 2d76de29ff

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

@ -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;
}
}
}

Loading…
Cancel
Save