|
| 1 | +/* |
| 2 | + * Copyright 2012-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.test.autoconfigure.orm.jpa; |
| 18 | + |
| 19 | +import javax.sql.DataSource; |
| 20 | + |
| 21 | +import org.junit.After; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import org.springframework.boot.test.util.EnvironmentTestUtils; |
| 25 | +import org.springframework.context.ConfigurableApplicationContext; |
| 26 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | + |
| 30 | +/** |
| 31 | + * Tests for {@link TestDatabaseAutoConfiguration}. |
| 32 | + * |
| 33 | + * @author Stephane Nicoll |
| 34 | + */ |
| 35 | +public class TestDatabaseAutoConfigurationTests { |
| 36 | + |
| 37 | + private ConfigurableApplicationContext context; |
| 38 | + |
| 39 | + @After |
| 40 | + public void closeContext() { |
| 41 | + if (this.context != null) { |
| 42 | + this.context.close(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void replaceWithNoDataSourceAvailable() { |
| 48 | + load(null); |
| 49 | + assertThat(this.context.getBeansOfType(DataSource.class)).isEmpty(); |
| 50 | + } |
| 51 | + |
| 52 | + public void load(Class<?> config, String... environment) { |
| 53 | + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
| 54 | + if (config != null) { |
| 55 | + ctx.register(config); |
| 56 | + } |
| 57 | + ctx.register(TestDatabaseAutoConfiguration.class); |
| 58 | + EnvironmentTestUtils.addEnvironment(ctx, environment); |
| 59 | + ctx.refresh(); |
| 60 | + this.context = ctx; |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments