|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.data.jpa;
|
18 | 18 |
|
19 |
| -import org.junit.After; |
20 | 19 | import org.junit.Test;
|
21 | 20 |
|
| 21 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
22 | 22 | import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
|
23 |
| -import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; |
24 | 23 | import org.springframework.boot.autoconfigure.data.jpa.city.City;
|
25 | 24 | import org.springframework.boot.autoconfigure.data.jpa.city.CityRepository;
|
26 | 25 | import org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration;
|
27 |
| -import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; |
| 26 | +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; |
28 | 27 | import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
|
| 28 | +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; |
29 | 29 | import org.springframework.context.annotation.Configuration;
|
| 30 | +import org.springframework.data.geo.Distance; |
30 | 31 | import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
|
| 32 | +import org.springframework.data.web.SortHandlerMethodArgumentResolver; |
31 | 33 | import org.springframework.format.support.FormattingConversionService;
|
32 |
| -import org.springframework.mock.web.MockServletContext; |
33 |
| -import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; |
34 | 34 | import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
35 | 35 |
|
36 | 36 | import static org.assertj.core.api.Assertions.assertThat;
|
|
40 | 40 | * {@link JpaRepositoriesAutoConfiguration}.
|
41 | 41 | *
|
42 | 42 | * @author Dave Syer
|
| 43 | + * @author Stephane Nicoll |
43 | 44 | */
|
44 | 45 | public class JpaWebAutoConfigurationTests {
|
45 | 46 |
|
46 |
| - private AnnotationConfigWebApplicationContext context; |
47 |
| - |
48 |
| - @After |
49 |
| - public void close() { |
50 |
| - this.context.close(); |
51 |
| - } |
| 47 | + private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() |
| 48 | + .withConfiguration( |
| 49 | + AutoConfigurations.of(DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class, |
| 50 | + JpaRepositoriesAutoConfiguration.class, SpringDataWebAutoConfiguration.class)) |
| 51 | + .withPropertyValues("spring.datasource.generate-unique-name=true"); |
52 | 52 |
|
53 | 53 | @Test
|
54 |
| - public void testDefaultRepositoryConfiguration() { |
55 |
| - this.context = new AnnotationConfigWebApplicationContext(); |
56 |
| - this.context.setServletContext(new MockServletContext()); |
57 |
| - this.context.register(TestConfiguration.class, EmbeddedDataSourceConfiguration.class, |
58 |
| - HibernateJpaAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class, |
59 |
| - SpringDataWebAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); |
60 |
| - this.context.refresh(); |
61 |
| - assertThat(this.context.getBean(CityRepository.class)).isNotNull(); |
62 |
| - assertThat(this.context.getBean(PageableHandlerMethodArgumentResolver.class)).isNotNull(); |
63 |
| - assertThat(this.context.getBean(FormattingConversionService.class).canConvert(Long.class, City.class)).isTrue(); |
| 54 | + public void springDataWebIsConfiguredWithJpaRepositories() { |
| 55 | + this.contextRunner.withUserConfiguration(TestConfiguration.class).run((context) -> { |
| 56 | + assertThat(context).hasSingleBean(CityRepository.class); |
| 57 | + assertThat(context).hasSingleBean(PageableHandlerMethodArgumentResolver.class); |
| 58 | + assertThat(context).hasSingleBean(SortHandlerMethodArgumentResolver.class); |
| 59 | + assertThat(context.getBean(FormattingConversionService.class).canConvert(String.class, Distance.class)) |
| 60 | + .isTrue(); |
| 61 | + }); |
64 | 62 | }
|
65 | 63 |
|
66 | 64 | @Configuration
|
67 | 65 | @TestAutoConfigurationPackage(City.class)
|
68 | 66 | @EnableWebMvc
|
69 |
| - protected static class TestConfiguration { |
| 67 | + static class TestConfiguration { |
70 | 68 |
|
71 | 69 | }
|
72 | 70 |
|
|
0 commit comments