Skip to content

Commit 81d6751

Browse files
committed
Use ApplicationContextRunner in ConditionalOnSingleCandidateTests
1 parent 1410ef2 commit 81d6751

File tree

1 file changed

+71
-86
lines changed

1 file changed

+71
-86
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnSingleCandidateTests.java

Lines changed: 71 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,16 +16,14 @@
1616

1717
package org.springframework.boot.autoconfigure.condition;
1818

19-
import org.junit.jupiter.api.AfterEach;
2019
import org.junit.jupiter.api.Test;
2120

22-
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
21+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2322
import org.springframework.context.annotation.Bean;
2423
import org.springframework.context.annotation.Configuration;
2524
import org.springframework.context.annotation.Primary;
2625

2726
import static org.assertj.core.api.Assertions.assertThat;
28-
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
2927

3028
/**
3129
* Tests for {@link ConditionalOnSingleCandidate @ConditionalOnSingleCandidate}.
@@ -35,125 +33,112 @@
3533
*/
3634
class ConditionalOnSingleCandidateTests {
3735

38-
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
39-
40-
@AfterEach
41-
void close() {
42-
if (this.context != null) {
43-
this.context.close();
44-
}
45-
}
36+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
4637

4738
@Test
4839
void singleCandidateNoCandidate() {
49-
load(OnBeanSingleCandidateConfiguration.class);
50-
assertThat(this.context.containsBean("baz")).isFalse();
40+
this.contextRunner.withUserConfiguration(OnBeanSingleCandidateConfiguration.class)
41+
.run((context) -> assertThat(context).doesNotHaveBean("consumer"));
5142
}
5243

5344
@Test
5445
void singleCandidateOneCandidate() {
55-
load(FooConfiguration.class, OnBeanSingleCandidateConfiguration.class);
56-
assertThat(this.context.containsBean("baz")).isTrue();
57-
assertThat(this.context.getBean("baz")).isEqualTo("foo");
46+
this.contextRunner.withUserConfiguration(AlphaConfiguration.class, OnBeanSingleCandidateConfiguration.class)
47+
.run((context) -> {
48+
assertThat(context).hasBean("consumer");
49+
assertThat(context.getBean("consumer")).isEqualTo("alpha");
50+
});
5851
}
5952

6053
@Test
6154
void singleCandidateInAncestorsOneCandidateInCurrent() {
62-
load();
63-
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
64-
child.register(FooConfiguration.class, OnBeanSingleCandidateInAncestorsConfiguration.class);
65-
child.setParent(this.context);
66-
child.refresh();
67-
assertThat(child.containsBean("baz")).isFalse();
68-
child.close();
55+
this.contextRunner.run((parent) -> this.contextRunner
56+
.withUserConfiguration(AlphaConfiguration.class, OnBeanSingleCandidateInAncestorsConfiguration.class)
57+
.withParent(parent).run((child) -> assertThat(child).doesNotHaveBean("consumer")));
6958
}
7059

7160
@Test
7261
void singleCandidateInAncestorsOneCandidateInParent() {
73-
load(FooConfiguration.class);
74-
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
75-
child.register(OnBeanSingleCandidateInAncestorsConfiguration.class);
76-
child.setParent(this.context);
77-
child.refresh();
78-
assertThat(child.containsBean("baz")).isTrue();
79-
assertThat(child.getBean("baz")).isEqualTo("foo");
80-
child.close();
62+
this.contextRunner.withUserConfiguration(AlphaConfiguration.class)
63+
.run((parent) -> this.contextRunner
64+
.withUserConfiguration(OnBeanSingleCandidateInAncestorsConfiguration.class).withParent(parent)
65+
.run((child) -> {
66+
assertThat(child).hasBean("consumer");
67+
assertThat(child.getBean("consumer")).isEqualTo("alpha");
68+
}));
8169
}
8270

8371
@Test
8472
void singleCandidateInAncestorsOneCandidateInGrandparent() {
85-
load(FooConfiguration.class);
86-
AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
87-
parent.setParent(this.context);
88-
parent.refresh();
89-
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
90-
child.register(OnBeanSingleCandidateInAncestorsConfiguration.class);
91-
child.setParent(parent);
92-
child.refresh();
93-
assertThat(child.containsBean("baz")).isTrue();
94-
assertThat(child.getBean("baz")).isEqualTo("foo");
95-
child.close();
96-
parent.close();
73+
this.contextRunner.withUserConfiguration(AlphaConfiguration.class)
74+
.run((grandparent) -> this.contextRunner.withParent(grandparent)
75+
.run((parent) -> this.contextRunner
76+
.withUserConfiguration(OnBeanSingleCandidateInAncestorsConfiguration.class)
77+
.withParent(parent).run((child) -> {
78+
assertThat(child).hasBean("consumer");
79+
assertThat(child.getBean("consumer")).isEqualTo("alpha");
80+
})));
9781
}
9882

9983
@Test
10084
void singleCandidateMultipleCandidates() {
101-
load(FooConfiguration.class, BarConfiguration.class, OnBeanSingleCandidateConfiguration.class);
102-
assertThat(this.context.containsBean("baz")).isFalse();
85+
this.contextRunner
86+
.withUserConfiguration(AlphaConfiguration.class, BravoConfiguration.class,
87+
OnBeanSingleCandidateConfiguration.class)
88+
.run((context) -> assertThat(context).doesNotHaveBean("consumer"));
10389
}
10490

10591
@Test
10692
void singleCandidateMultipleCandidatesOnePrimary() {
107-
load(FooPrimaryConfiguration.class, BarConfiguration.class, OnBeanSingleCandidateConfiguration.class);
108-
assertThat(this.context.containsBean("baz")).isTrue();
109-
assertThat(this.context.getBean("baz")).isEqualTo("foo");
93+
this.contextRunner.withUserConfiguration(AlphaPrimaryConfiguration.class, BravoConfiguration.class,
94+
OnBeanSingleCandidateConfiguration.class).run((context) -> {
95+
assertThat(context).hasBean("consumer");
96+
assertThat(context.getBean("consumer")).isEqualTo("alpha");
97+
});
11098
}
11199

112100
@Test
113101
void singleCandidateMultipleCandidatesMultiplePrimary() {
114-
load(FooPrimaryConfiguration.class, BarPrimaryConfiguration.class, OnBeanSingleCandidateConfiguration.class);
115-
assertThat(this.context.containsBean("baz")).isFalse();
102+
this.contextRunner
103+
.withUserConfiguration(AlphaPrimaryConfiguration.class, BravoPrimaryConfiguration.class,
104+
OnBeanSingleCandidateConfiguration.class)
105+
.run((context) -> assertThat(context).doesNotHaveBean("consumer"));
116106
}
117107

118108
@Test
119109
void invalidAnnotationTwoTypes() {
120-
assertThatIllegalStateException().isThrownBy(() -> load(OnBeanSingleCandidateTwoTypesConfiguration.class))
121-
.withCauseInstanceOf(IllegalArgumentException.class)
122-
.withMessageContaining(OnBeanSingleCandidateTwoTypesConfiguration.class.getName());
110+
this.contextRunner.withUserConfiguration(OnBeanSingleCandidateTwoTypesConfiguration.class).run((context) -> {
111+
assertThat(context).hasFailed();
112+
assertThat(context).getFailure().hasCauseInstanceOf(IllegalArgumentException.class)
113+
.hasMessageContaining(OnBeanSingleCandidateTwoTypesConfiguration.class.getName());
114+
});
123115
}
124116

125117
@Test
126118
void invalidAnnotationNoType() {
127-
assertThatIllegalStateException().isThrownBy(() -> load(OnBeanSingleCandidateNoTypeConfiguration.class))
128-
.withCauseInstanceOf(IllegalArgumentException.class)
129-
.withMessageContaining(OnBeanSingleCandidateNoTypeConfiguration.class.getName());
119+
this.contextRunner.withUserConfiguration(OnBeanSingleCandidateNoTypeConfiguration.class).run((context) -> {
120+
assertThat(context).hasFailed();
121+
assertThat(context).getFailure().hasCauseInstanceOf(IllegalArgumentException.class)
122+
.hasMessageContaining(OnBeanSingleCandidateNoTypeConfiguration.class.getName());
123+
});
130124
}
131125

132126
@Test
133127
void singleCandidateMultipleCandidatesInContextHierarchy() {
134-
load(FooPrimaryConfiguration.class, BarConfiguration.class);
135-
try (AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext()) {
136-
child.setParent(this.context);
137-
child.register(OnBeanSingleCandidateConfiguration.class);
138-
child.refresh();
139-
assertThat(child.containsBean("baz")).isTrue();
140-
assertThat(child.getBean("baz")).isEqualTo("foo");
141-
}
142-
}
143-
144-
private void load(Class<?>... classes) {
145-
if (classes.length > 0) {
146-
this.context.register(classes);
147-
}
148-
this.context.refresh();
128+
this.contextRunner.withUserConfiguration(AlphaPrimaryConfiguration.class, BravoConfiguration.class)
129+
.run((parent) -> this.contextRunner.withUserConfiguration(OnBeanSingleCandidateConfiguration.class)
130+
.withParent(parent).run((child) -> {
131+
assertThat(child).hasBean("consumer");
132+
assertThat(child.getBean("consumer")).isEqualTo("alpha");
133+
}));
149134
}
150135

151136
@Configuration(proxyBeanMethods = false)
152137
@ConditionalOnSingleCandidate(String.class)
153138
static class OnBeanSingleCandidateConfiguration {
154139

155140
@Bean
156-
String baz(String s) {
141+
String consumer(String s) {
157142
return s;
158143
}
159144

@@ -164,7 +149,7 @@ String baz(String s) {
164149
static class OnBeanSingleCandidateInAncestorsConfiguration {
165150

166151
@Bean
167-
String baz(String s) {
152+
String consumer(String s) {
168153
return s;
169154
}
170155

@@ -183,43 +168,43 @@ static class OnBeanSingleCandidateNoTypeConfiguration {
183168
}
184169

185170
@Configuration(proxyBeanMethods = false)
186-
static class FooConfiguration {
171+
static class AlphaConfiguration {
187172

188173
@Bean
189-
String foo() {
190-
return "foo";
174+
String alpha() {
175+
return "alpha";
191176
}
192177

193178
}
194179

195180
@Configuration(proxyBeanMethods = false)
196-
static class FooPrimaryConfiguration {
181+
static class AlphaPrimaryConfiguration {
197182

198183
@Bean
199184
@Primary
200-
String foo() {
201-
return "foo";
185+
String alpha() {
186+
return "alpha";
202187
}
203188

204189
}
205190

206191
@Configuration(proxyBeanMethods = false)
207-
static class BarConfiguration {
192+
static class BravoConfiguration {
208193

209194
@Bean
210-
String bar() {
211-
return "bar";
195+
String bravo() {
196+
return "bravo";
212197
}
213198

214199
}
215200

216201
@Configuration(proxyBeanMethods = false)
217-
static class BarPrimaryConfiguration {
202+
static class BravoPrimaryConfiguration {
218203

219204
@Bean
220205
@Primary
221-
String bar() {
222-
return "bar";
206+
String bravo() {
207+
return "bravo";
223208
}
224209

225210
}

0 commit comments

Comments
 (0)