Skip to content

Commit 677d52f

Browse files
committed
Polish "Add Spring Data Web configuration properties"
Closes gh-9339
1 parent e9ac41f commit 677d52f

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfigurationTests.java

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.junit.Test;
2323

2424
import org.springframework.boot.test.util.TestPropertyValues;
25-
import org.springframework.context.ConfigurableApplicationContext;
2625
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2726
import org.springframework.data.domain.PageRequest;
2827
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
@@ -38,10 +37,11 @@
3837
*
3938
* @author Andy Wilkinson
4039
* @author Vedran Pavic
40+
* @author Stephane Nicoll
4141
*/
4242
public class SpringDataWebAutoConfigurationTests {
4343

44-
private ConfigurableApplicationContext context;
44+
private AnnotationConfigWebApplicationContext context;
4545

4646
@After
4747
public void after() {
@@ -52,39 +52,33 @@ public void after() {
5252

5353
@Test
5454
public void webSupportIsAutoConfiguredInWebApplicationContexts() {
55-
this.context = new AnnotationConfigWebApplicationContext();
56-
((AnnotationConfigWebApplicationContext) this.context)
57-
.register(SpringDataWebAutoConfiguration.class);
58-
this.context.refresh();
59-
((AnnotationConfigWebApplicationContext) this.context)
60-
.setServletContext(new MockServletContext());
55+
load();
56+
this.context.setServletContext(new MockServletContext());
6157
Map<String, PageableHandlerMethodArgumentResolver> beans = this.context
6258
.getBeansOfType(PageableHandlerMethodArgumentResolver.class);
6359
assertThat(beans).hasSize(1);
6460
}
6561

6662
@Test
6763
public void autoConfigurationBacksOffInNonWebApplicationContexts() {
68-
this.context = new AnnotationConfigApplicationContext();
69-
((AnnotationConfigApplicationContext) this.context)
70-
.register(SpringDataWebAutoConfiguration.class);
71-
this.context.refresh();
72-
Map<String, PageableHandlerMethodArgumentResolver> beans = this.context
73-
.getBeansOfType(PageableHandlerMethodArgumentResolver.class);
74-
assertThat(beans).isEmpty();
64+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
65+
ctx.register(SpringDataWebAutoConfiguration.class);
66+
try {
67+
ctx.refresh();
68+
Map<String, PageableHandlerMethodArgumentResolver> beans = ctx
69+
.getBeansOfType(PageableHandlerMethodArgumentResolver.class);
70+
assertThat(beans).isEmpty();
71+
}
72+
finally {
73+
ctx.close();
74+
}
7575
}
7676

7777
@Test
78-
public void pageable() {
79-
this.context = new AnnotationConfigWebApplicationContext();
80-
TestPropertyValues
81-
.of("spring.data.web.pageable.page-parameter=p",
82-
"spring.data.web.pageable.size-parameter=s",
83-
"spring.data.web.pageable.default-page-size=10")
84-
.applyTo(this.context);
85-
((AnnotationConfigWebApplicationContext) this.context)
86-
.register(SpringDataWebAutoConfiguration.class);
87-
this.context.refresh();
78+
public void customizePageable() {
79+
load("spring.data.web.pageable.page-parameter=p",
80+
"spring.data.web.pageable.size-parameter=s",
81+
"spring.data.web.pageable.default-page-size=10");
8882
PageableHandlerMethodArgumentResolver argumentResolver = this.context
8983
.getBean(PageableHandlerMethodArgumentResolver.class);
9084
assertThat(ReflectionTestUtils.getField(argumentResolver, "pageParameterName"))
@@ -96,17 +90,21 @@ public void pageable() {
9690
}
9791

9892
@Test
99-
public void sort() {
100-
this.context = new AnnotationConfigWebApplicationContext();
101-
TestPropertyValues.of("spring.data.web.sort.sort-parameter=s")
102-
.applyTo(this.context);
103-
((AnnotationConfigWebApplicationContext) this.context)
104-
.register(SpringDataWebAutoConfiguration.class);
105-
this.context.refresh();
93+
public void customizeSort() {
94+
load("spring.data.web.sort.sort-parameter=s");
10695
SortHandlerMethodArgumentResolver argumentResolver = this.context
10796
.getBean(SortHandlerMethodArgumentResolver.class);
10897
assertThat(ReflectionTestUtils.getField(argumentResolver, "sortParameter"))
10998
.isEqualTo("s");
11099
}
111100

101+
private void load(String... environment) {
102+
AnnotationConfigWebApplicationContext ctx =
103+
new AnnotationConfigWebApplicationContext();
104+
TestPropertyValues.of(environment).applyTo(ctx);
105+
ctx.register(SpringDataWebAutoConfiguration.class);
106+
ctx.refresh();
107+
this.context = ctx;
108+
}
109+
112110
}

0 commit comments

Comments
 (0)