diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java index bd7ce321ff..5a585918c6 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,16 +16,12 @@ package org.springframework.boot.autoconfigure.jdbc; -import java.util.Random; - import javax.sql.DataSource; -import org.junit.After; import org.junit.Test; -import org.springframework.boot.test.util.TestPropertyValues; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; @@ -46,129 +42,121 @@ import static org.mockito.Mockito.mock; */ public class JdbcTemplateAutoConfigurationTests { - private ConfigurableApplicationContext context; - - @After - public void restore() { - if (this.context != null) { - this.context.close(); - } - } + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withPropertyValues("spring.datasource.initialization-mode=never", + "spring.datasource.generate-unique-name=true") + .withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class, + JdbcTemplateAutoConfiguration.class)); @Test public void testJdbcTemplateExists() { - load(); - assertThat(this.context.getBeansOfType(JdbcOperations.class)).hasSize(1); - JdbcTemplate jdbcTemplate = this.context.getBean(JdbcTemplate.class); - assertThat(jdbcTemplate.getDataSource()) - .isEqualTo(this.context.getBean(DataSource.class)); - assertThat(jdbcTemplate.getFetchSize()).isEqualTo(-1); - assertThat(jdbcTemplate.getQueryTimeout()).isEqualTo(-1); - assertThat(jdbcTemplate.getMaxRows()).isEqualTo(-1); + this.contextRunner.run((context) -> { + assertThat(context).hasSingleBean(JdbcOperations.class); + JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class); + assertThat(jdbcTemplate.getDataSource()) + .isEqualTo(context.getBean(DataSource.class)); + assertThat(jdbcTemplate.getFetchSize()).isEqualTo(-1); + assertThat(jdbcTemplate.getQueryTimeout()).isEqualTo(-1); + assertThat(jdbcTemplate.getMaxRows()).isEqualTo(-1); + }); } @Test public void testJdbcTemplateWithCustomProperties() { - load("spring.jdbc.template.fetch-size:100", + this.contextRunner.withPropertyValues("spring.jdbc.template.fetch-size:100", "spring.jdbc.template.query-timeout:60", - "spring.jdbc.template.max-rows:1000"); - JdbcTemplate jdbcTemplate = this.context.getBean(JdbcTemplate.class); - assertThat(jdbcTemplate).isNotNull(); - assertThat(jdbcTemplate.getDataSource()).isNotNull(); - assertThat(jdbcTemplate.getFetchSize()).isEqualTo(100); - assertThat(jdbcTemplate.getQueryTimeout()).isEqualTo(60); - assertThat(jdbcTemplate.getMaxRows()).isEqualTo(1000); + "spring.jdbc.template.max-rows:1000").run((context) -> { + assertThat(context).hasSingleBean(JdbcOperations.class); + JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class); + assertThat(jdbcTemplate.getDataSource()).isNotNull(); + assertThat(jdbcTemplate.getFetchSize()).isEqualTo(100); + assertThat(jdbcTemplate.getQueryTimeout()).isEqualTo(60); + assertThat(jdbcTemplate.getMaxRows()).isEqualTo(1000); + }); } @Test public void testJdbcTemplateExistsWithCustomDataSource() { - load(TestDataSourceConfiguration.class); - assertThat(this.context.getBeansOfType(JdbcOperations.class)).hasSize(1); - JdbcTemplate jdbcTemplate = this.context.getBean(JdbcTemplate.class); - assertThat(jdbcTemplate).isNotNull(); - assertThat(jdbcTemplate.getDataSource()) - .isEqualTo(this.context.getBean("customDataSource")); + this.contextRunner.withUserConfiguration(TestDataSourceConfiguration.class) + .run((context) -> { + assertThat(context).hasSingleBean(JdbcOperations.class); + JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class); + assertThat(jdbcTemplate.getDataSource()) + .isEqualTo(context.getBean("customDataSource")); + }); } @Test public void testNamedParameterJdbcTemplateExists() { - load(); - assertThat(this.context.getBeansOfType(NamedParameterJdbcOperations.class)) - .hasSize(1); - NamedParameterJdbcTemplate namedParameterJdbcTemplate = this.context - .getBean(NamedParameterJdbcTemplate.class); - assertThat(namedParameterJdbcTemplate.getJdbcOperations()) - .isEqualTo(this.context.getBean(JdbcOperations.class)); + this.contextRunner.run((context) -> { + assertThat(context).hasSingleBean(NamedParameterJdbcOperations.class); + NamedParameterJdbcTemplate namedParameterJdbcTemplate = context + .getBean(NamedParameterJdbcTemplate.class); + assertThat(namedParameterJdbcTemplate.getJdbcOperations()) + .isEqualTo(context.getBean(JdbcOperations.class)); + }); } @Test public void testMultiDataSource() { - load(MultiDataSourceConfiguration.class); - assertThat(this.context.getBeansOfType(JdbcOperations.class)).isEmpty(); - assertThat(this.context.getBeansOfType(NamedParameterJdbcOperations.class)) - .isEmpty(); + this.contextRunner.withUserConfiguration(MultiDataSourceConfiguration.class) + .run((context) -> { + assertThat(context).doesNotHaveBean(JdbcOperations.class); + assertThat(context) + .doesNotHaveBean(NamedParameterJdbcOperations.class); + }); } @Test public void testMultiJdbcTemplate() { - load(MultiJdbcTemplateConfiguration.class); - assertThat(this.context.getBeansOfType(NamedParameterJdbcOperations.class)) - .isEmpty(); + this.contextRunner.withUserConfiguration(MultiJdbcTemplateConfiguration.class) + .run((context) -> assertThat(context) + .doesNotHaveBean(NamedParameterJdbcOperations.class)); } @Test public void testMultiDataSourceUsingPrimary() { - load(MultiDataSourceUsingPrimaryConfiguration.class); - assertThat(this.context.getBeansOfType(JdbcOperations.class)).hasSize(1); - assertThat(this.context.getBeansOfType(NamedParameterJdbcOperations.class)) - .hasSize(1); - assertThat(this.context.getBean(JdbcTemplate.class).getDataSource()) - .isEqualTo(this.context.getBean("test1DataSource")); + this.contextRunner + .withUserConfiguration(MultiDataSourceUsingPrimaryConfiguration.class) + .run((context) -> { + assertThat(context).hasSingleBean(JdbcOperations.class); + assertThat(context).hasSingleBean(NamedParameterJdbcOperations.class); + assertThat(context.getBean(JdbcTemplate.class).getDataSource()) + .isEqualTo(context.getBean("test1DataSource")); + }); } @Test public void testMultiJdbcTemplateUsingPrimary() { - load(MultiJdbcTemplateUsingPrimaryConfiguration.class); - assertThat(this.context.getBeansOfType(NamedParameterJdbcOperations.class)) - .hasSize(1); - assertThat(this.context.getBean(NamedParameterJdbcTemplate.class) - .getJdbcOperations()).isEqualTo(this.context.getBean("test1Template")); + this.contextRunner + .withUserConfiguration(MultiJdbcTemplateUsingPrimaryConfiguration.class) + .run((context) -> { + assertThat(context).hasSingleBean(NamedParameterJdbcOperations.class); + assertThat(context.getBean(NamedParameterJdbcTemplate.class) + .getJdbcOperations()) + .isEqualTo(context.getBean("test1Template")); + }); } @Test public void testExistingCustomJdbcTemplate() { - load(CustomConfiguration.class); - assertThat(this.context.getBeansOfType(JdbcOperations.class)).hasSize(1); - assertThat(this.context.getBean(JdbcOperations.class)) - .isEqualTo(this.context.getBean("customJdbcOperations")); + this.contextRunner.withUserConfiguration(CustomConfiguration.class) + .run((context) -> { + assertThat(context).hasSingleBean(JdbcOperations.class); + assertThat(context.getBean(JdbcOperations.class)) + .isEqualTo(context.getBean("customJdbcOperations")); + }); } @Test public void testExistingCustomNamedParameterJdbcTemplate() { - load(CustomConfiguration.class); - assertThat(this.context.getBeansOfType(NamedParameterJdbcOperations.class)) - .hasSize(1); - assertThat(this.context.getBean(NamedParameterJdbcOperations.class)) - .isEqualTo(this.context.getBean("customNamedParameterJdbcOperations")); - } - - public void load(String... environment) { - load(null, environment); - } - - public void load(Class config, String... environment) { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - TestPropertyValues.of("spring.datasource.initialization-mode:never", - "spring.datasource.url:jdbc:hsqldb:mem:testdb-" + new Random().nextInt()) - .applyTo(ctx); - TestPropertyValues.of(environment).applyTo(ctx); - if (config != null) { - ctx.register(config); - } - ctx.register(DataSourceAutoConfiguration.class, - JdbcTemplateAutoConfiguration.class); - ctx.refresh(); - this.context = ctx; + this.contextRunner.withUserConfiguration(CustomConfiguration.class) + .run((context) -> { + assertThat(context).hasSingleBean(NamedParameterJdbcOperations.class); + assertThat(context.getBean(NamedParameterJdbcOperations.class)) + .isEqualTo(context + .getBean("customNamedParameterJdbcOperations")); + }); } @Configuration