From 9a457527fdeea7d06298d0388773a49c63c38379 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 9 Jun 2020 16:00:50 +0200 Subject: [PATCH] Polish test --- .../jpa/JpaWebAutoConfigurationTests.java | 42 +++++++++---------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/jpa/JpaWebAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/jpa/JpaWebAutoConfigurationTests.java index 70fa9b33cc..a390799807 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/jpa/JpaWebAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/jpa/JpaWebAutoConfigurationTests.java @@ -16,21 +16,21 @@ package org.springframework.boot.autoconfigure.data.jpa; -import org.junit.After; import org.junit.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage; -import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.data.jpa.city.City; import org.springframework.boot.autoconfigure.data.jpa.city.CityRepository; import org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration; -import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.context.annotation.Configuration; +import org.springframework.data.geo.Distance; import org.springframework.data.web.PageableHandlerMethodArgumentResolver; +import org.springframework.data.web.SortHandlerMethodArgumentResolver; import org.springframework.format.support.FormattingConversionService; -import org.springframework.mock.web.MockServletContext; -import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import static org.assertj.core.api.Assertions.assertThat; @@ -40,33 +40,31 @@ import static org.assertj.core.api.Assertions.assertThat; * {@link JpaRepositoriesAutoConfiguration}. * * @author Dave Syer + * @author Stephane Nicoll */ public class JpaWebAutoConfigurationTests { - private AnnotationConfigWebApplicationContext context; - - @After - public void close() { - this.context.close(); - } + private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() + .withConfiguration( + AutoConfigurations.of(DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class, + JpaRepositoriesAutoConfiguration.class, SpringDataWebAutoConfiguration.class)) + .withPropertyValues("spring.datasource.generate-unique-name=true"); @Test - public void testDefaultRepositoryConfiguration() { - this.context = new AnnotationConfigWebApplicationContext(); - this.context.setServletContext(new MockServletContext()); - this.context.register(TestConfiguration.class, EmbeddedDataSourceConfiguration.class, - HibernateJpaAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class, - SpringDataWebAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); - assertThat(this.context.getBean(CityRepository.class)).isNotNull(); - assertThat(this.context.getBean(PageableHandlerMethodArgumentResolver.class)).isNotNull(); - assertThat(this.context.getBean(FormattingConversionService.class).canConvert(Long.class, City.class)).isTrue(); + public void springDataWebIsConfiguredWithJpaRepositories() { + this.contextRunner.withUserConfiguration(TestConfiguration.class).run((context) -> { + assertThat(context).hasSingleBean(CityRepository.class); + assertThat(context).hasSingleBean(PageableHandlerMethodArgumentResolver.class); + assertThat(context).hasSingleBean(SortHandlerMethodArgumentResolver.class); + assertThat(context.getBean(FormattingConversionService.class).canConvert(String.class, Distance.class)) + .isTrue(); + }); } @Configuration @TestAutoConfigurationPackage(City.class) @EnableWebMvc - protected static class TestConfiguration { + static class TestConfiguration { }