|
17 | 17 | package org.springframework.boot.autoconfigure.h2;
|
18 | 18 |
|
19 | 19 | import java.sql.Connection;
|
| 20 | +import java.sql.SQLException; |
20 | 21 |
|
21 | 22 | import javax.sql.DataSource;
|
22 | 23 |
|
23 | 24 | import org.junit.jupiter.api.Test;
|
24 | 25 | import org.junit.jupiter.api.extension.ExtendWith;
|
| 26 | +import org.mockito.BDDMockito; |
25 | 27 |
|
26 | 28 | import org.springframework.beans.BeansException;
|
27 | 29 | import org.springframework.beans.factory.BeanCreationException;
|
|
31 | 33 | import org.springframework.boot.test.system.CapturedOutput;
|
32 | 34 | import org.springframework.boot.test.system.OutputCaptureExtension;
|
33 | 35 | import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
| 36 | +import org.springframework.context.annotation.Bean; |
| 37 | +import org.springframework.context.annotation.Configuration; |
34 | 38 |
|
35 | 39 | import static org.assertj.core.api.Assertions.assertThat;
|
| 40 | +import static org.mockito.Mockito.mock; |
36 | 41 |
|
37 | 42 | /**
|
38 | 43 | * Tests for {@link H2ConsoleAutoConfiguration}
|
@@ -117,4 +122,25 @@ void dataSourceUrlIsLoggedWhenAvailable(CapturedOutput output) throws BeansExcep
|
117 | 122 | });
|
118 | 123 | }
|
119 | 124 |
|
| 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 | + |
120 | 146 | }
|
0 commit comments