|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2021 the original author or authors. |
| 2 | + * Copyright 2012-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.h2;
|
18 | 18 |
|
| 19 | +import java.net.URL; |
| 20 | +import java.net.URLClassLoader; |
19 | 21 | import java.sql.Connection;
|
20 | 22 | import java.sql.DatabaseMetaData;
|
21 | 23 | import java.sql.SQLException;
|
|
24 | 26 |
|
25 | 27 | import org.junit.jupiter.api.Test;
|
26 | 28 | import org.junit.jupiter.api.extension.ExtendWith;
|
| 29 | +import org.mockito.invocation.InvocationOnMock; |
| 30 | +import org.mockito.stubbing.Answer; |
27 | 31 |
|
28 | 32 | import org.springframework.beans.factory.BeanCreationException;
|
29 | 33 | import org.springframework.boot.autoconfigure.AutoConfigurations;
|
@@ -137,7 +141,8 @@ void noDataSourceIsLoggedWhenNoneAvailable(CapturedOutput output) {
|
137 | 141 | @Test
|
138 | 142 | @ExtendWith(OutputCaptureExtension.class)
|
139 | 143 | void allDataSourceUrlsAreLoggedWhenMultipleAvailable(CapturedOutput output) {
|
140 |
| - this.contextRunner |
| 144 | + ClassLoader webAppClassLoader = new URLClassLoader(new URL[0]); |
| 145 | + this.contextRunner.withClassLoader(webAppClassLoader) |
141 | 146 | .withUserConfiguration(FailingDataSourceConfiguration.class, MultiDataSourceConfiguration.class)
|
142 | 147 | .withPropertyValues("spring.h2.console.enabled=true").run((context) -> assertThat(output).contains(
|
143 | 148 | "H2 console available at '/h2-console'. Databases available at 'someJdbcUrl', 'anotherJdbcUrl'"));
|
@@ -179,9 +184,20 @@ DataSource someDataSource() throws SQLException {
|
179 | 184 |
|
180 | 185 | private DataSource mockDataSource(String url) throws SQLException {
|
181 | 186 | DataSource dataSource = mock(DataSource.class);
|
182 |
| - given(dataSource.getConnection()).willReturn(mock(Connection.class)); |
183 |
| - given(dataSource.getConnection().getMetaData()).willReturn(mock(DatabaseMetaData.class)); |
184 |
| - given(dataSource.getConnection().getMetaData().getURL()).willReturn(url); |
| 187 | + given(dataSource.getConnection()).will(new Answer<Connection>() { |
| 188 | + |
| 189 | + @Override |
| 190 | + public Connection answer(InvocationOnMock invocation) throws Throwable { |
| 191 | + assertThat(Thread.currentThread().getContextClassLoader()).isEqualTo(getClass().getClassLoader()); |
| 192 | + Connection connection = mock(Connection.class); |
| 193 | + DatabaseMetaData metadata = mock(DatabaseMetaData.class); |
| 194 | + given(connection.getMetaData()).willReturn(metadata); |
| 195 | + given(metadata.getURL()).willReturn(url); |
| 196 | + return connection; |
| 197 | + } |
| 198 | + |
| 199 | + }); |
| 200 | + |
185 | 201 | return dataSource;
|
186 | 202 | }
|
187 | 203 |
|
|
0 commit comments