From c0f158dffde52f726c0938065114b747afab07a0 Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Thu, 1 Oct 2020 18:16:11 -0700 Subject: [PATCH] Polish "Do not fail if h2Console bean cannot connect to db" See gh-23566 --- .../h2/H2ConsoleAutoConfiguration.java | 1 - .../h2/H2ConsoleAutoConfigurationTests.java | 19 +++++++++---------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java index ca3fb981b7..60174667fb 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java @@ -17,7 +17,6 @@ package org.springframework.boot.autoconfigure.h2; import java.sql.Connection; -import java.sql.SQLException; import javax.sql.DataSource; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java index c6568c8182..6d9b2d90c7 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java @@ -23,7 +23,6 @@ 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; @@ -37,6 +36,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; /** @@ -45,6 +45,7 @@ import static org.mockito.Mockito.mock; * @author Andy Wilkinson * @author Marten Deinum * @author Stephane Nicoll + * @author Shraddha Yeole */ class H2ConsoleAutoConfigurationTests { @@ -123,24 +124,22 @@ class H2ConsoleAutoConfigurationTests { } @Test - void testDataSource() { - this.contextRunner.withUserConfiguration(DataSourceAvailable.class) - .withPropertyValues("spring.h2.console.enabled=true").run((context) -> { - assertThat(context.isRunning()).isTrue(); - }); + void h2ConsoleShouldNotFailIfDatabaseConnectionFails() { + this.contextRunner.withUserConfiguration(CustomDataSourceConfiguration.class) + .withPropertyValues("spring.h2.console.enabled=true") + .run((context) -> assertThat(context.isRunning()).isTrue()); } @Configuration(proxyBeanMethods = false) - static class DataSourceAvailable { + static class CustomDataSourceConfiguration { @Bean - public DataSource dataSource() throws SQLException { + DataSource dataSource() throws SQLException { DataSource dataSource = mock(DataSource.class); - BDDMockito.when(dataSource.getConnection()).thenThrow(IllegalStateException.class); + given(dataSource.getConnection()).willThrow(IllegalStateException.class); return dataSource; } } - }