Skip to content

Commit c8c32cf

Browse files
committed
Migrate tests to ApplicationContextRunner
1 parent cced351 commit c8c32cf

File tree

2 files changed

+79
-100
lines changed

2 files changed

+79
-100
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfigurationTests.java

Lines changed: 69 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@
2121
import java.util.Set;
2222

2323
import com.mongodb.MongoClient;
24-
import org.junit.After;
2524
import org.junit.Test;
2625

2726
import org.springframework.beans.factory.BeanCreationException;
2827
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
28+
import org.springframework.boot.autoconfigure.AutoConfigurations;
2929
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
3030
import org.springframework.boot.autoconfigure.data.mongo.city.City;
3131
import org.springframework.boot.autoconfigure.data.mongo.country.Country;
3232
import org.springframework.boot.autoconfigure.domain.EntityScan;
3333
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
34-
import org.springframework.boot.test.util.TestPropertyValues;
34+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3535
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3636
import org.springframework.context.annotation.Bean;
3737
import org.springframework.context.annotation.Configuration;
@@ -48,7 +48,6 @@
4848
import org.springframework.test.util.ReflectionTestUtils;
4949

5050
import static org.assertj.core.api.Assertions.assertThat;
51-
import static org.junit.Assert.fail;
5251

5352
/**
5453
* Tests for {@link MongoDataAutoConfiguration}.
@@ -58,125 +57,111 @@
5857
*/
5958
public class MongoDataAutoConfigurationTests {
6059

61-
private AnnotationConfigApplicationContext context;
62-
63-
@After
64-
public void close() {
65-
if (this.context != null) {
66-
this.context.close();
67-
}
68-
}
60+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
61+
.withConfiguration(AutoConfigurations.of(
62+
PropertyPlaceholderAutoConfiguration.class,
63+
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class));
6964

7065
@Test
7166
public void templateExists() {
72-
this.context = new AnnotationConfigApplicationContext(
73-
PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class,
74-
MongoDataAutoConfiguration.class);
75-
assertThat(this.context.getBeanNamesForType(MongoTemplate.class).length)
76-
.isEqualTo(1);
67+
this.contextRunner
68+
.run((context) -> assertThat(context).hasSingleBean(MongoTemplate.class));
7769
}
7870

7971
@Test
8072
public void gridFsTemplateExists() {
81-
this.context = new AnnotationConfigApplicationContext();
82-
TestPropertyValues.of("spring.data.mongodb.gridFsDatabase:grid")
83-
.applyTo(this.context);
84-
this.context.register(PropertyPlaceholderAutoConfiguration.class,
85-
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class);
86-
this.context.refresh();
87-
assertThat(this.context.getBeanNamesForType(GridFsTemplate.class).length)
88-
.isEqualTo(1);
73+
this.contextRunner.withPropertyValues("spring.data.mongodb.gridFsDatabase:grid")
74+
.run((context) -> assertThat(context)
75+
.hasSingleBean(GridFsTemplate.class));
8976
}
9077

9178
@Test
9279
public void customConversions() {
93-
this.context = new AnnotationConfigApplicationContext();
94-
this.context.register(CustomConversionsConfig.class);
95-
this.context.register(PropertyPlaceholderAutoConfiguration.class,
96-
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class);
97-
this.context.refresh();
98-
MongoTemplate template = this.context.getBean(MongoTemplate.class);
99-
assertThat(template.getConverter().getConversionService()
100-
.canConvert(MongoClient.class, Boolean.class)).isTrue();
80+
this.contextRunner.withUserConfiguration(CustomConversionsConfig.class)
81+
.run((context) -> {
82+
MongoTemplate template = context.getBean(MongoTemplate.class);
83+
assertThat(template.getConverter().getConversionService()
84+
.canConvert(MongoClient.class, Boolean.class)).isTrue();
85+
});
10186
}
10287

10388
@Test
10489
public void usesAutoConfigurationPackageToPickUpDocumentTypes() {
105-
this.context = new AnnotationConfigApplicationContext();
90+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
10691
String cityPackage = City.class.getPackage().getName();
107-
AutoConfigurationPackages.register(this.context, cityPackage);
108-
this.context.register(MongoAutoConfiguration.class,
109-
MongoDataAutoConfiguration.class);
110-
this.context.refresh();
111-
assertDomainTypesDiscovered(this.context.getBean(MongoMappingContext.class),
112-
City.class);
92+
AutoConfigurationPackages.register(context, cityPackage);
93+
context.register(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class);
94+
try {
95+
context.refresh();
96+
assertDomainTypesDiscovered(context.getBean(MongoMappingContext.class),
97+
City.class);
98+
}
99+
finally {
100+
context.close();
101+
}
113102
}
114103

115104
@Test
116105
public void defaultFieldNamingStrategy() {
117-
testFieldNamingStrategy(null, PropertyNameFieldNamingStrategy.class);
106+
this.contextRunner.run((context) -> {
107+
MongoMappingContext mappingContext = context
108+
.getBean(MongoMappingContext.class);
109+
FieldNamingStrategy fieldNamingStrategy = (FieldNamingStrategy) ReflectionTestUtils
110+
.getField(mappingContext, "fieldNamingStrategy");
111+
assertThat(fieldNamingStrategy.getClass())
112+
.isEqualTo(PropertyNameFieldNamingStrategy.class);
113+
});
118114
}
119115

120116
@Test
121117
public void customFieldNamingStrategy() {
122-
testFieldNamingStrategy(CamelCaseAbbreviatingFieldNamingStrategy.class.getName(),
123-
CamelCaseAbbreviatingFieldNamingStrategy.class);
118+
this.contextRunner
119+
.withPropertyValues("spring.data.mongodb.field-naming-strategy:"
120+
+ CamelCaseAbbreviatingFieldNamingStrategy.class.getName())
121+
.run((context) -> {
122+
MongoMappingContext mappingContext = context
123+
.getBean(MongoMappingContext.class);
124+
FieldNamingStrategy fieldNamingStrategy = (FieldNamingStrategy) ReflectionTestUtils
125+
.getField(mappingContext, "fieldNamingStrategy");
126+
assertThat(fieldNamingStrategy.getClass())
127+
.isEqualTo(CamelCaseAbbreviatingFieldNamingStrategy.class);
128+
});
124129
}
125130

126131
@Test
127132
public void interfaceFieldNamingStrategy() {
128-
try {
129-
testFieldNamingStrategy(FieldNamingStrategy.class.getName(), null);
130-
fail("Create FieldNamingStrategy interface should fail");
131-
}
132-
// We seem to have an inconsistent exception, accept either
133-
catch (BeanCreationException ex) {
134-
// Expected
135-
}
133+
this.contextRunner
134+
.withPropertyValues("spring.data.mongodb.field-naming-strategy:"
135+
+ FieldNamingStrategy.class.getName())
136+
.run((context) -> assertThat(context).getFailure()
137+
.isInstanceOf(BeanCreationException.class));
136138
}
137139

138140
@Test
139141
@SuppressWarnings("unchecked")
140142
public void entityScanShouldSetInitialEntitySet() {
141-
this.context = new AnnotationConfigApplicationContext();
142-
this.context.register(EntityScanConfig.class,
143-
PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class,
144-
MongoDataAutoConfiguration.class);
145-
this.context.refresh();
146-
MongoMappingContext mappingContext = this.context
147-
.getBean(MongoMappingContext.class);
148-
Set<Class<?>> initialEntitySet = (Set<Class<?>>) ReflectionTestUtils
149-
.getField(mappingContext, "initialEntitySet");
150-
assertThat(initialEntitySet).containsOnly(City.class, Country.class);
143+
this.contextRunner.withUserConfiguration(EntityScanConfig.class)
144+
.run((context) -> {
145+
MongoMappingContext mappingContext = context
146+
.getBean(MongoMappingContext.class);
147+
Set<Class<?>> initialEntitySet = (Set<Class<?>>) ReflectionTestUtils
148+
.getField(mappingContext, "initialEntitySet");
149+
assertThat(initialEntitySet).containsOnly(City.class, Country.class);
150+
});
151+
151152
}
152153

153154
@Test
154155
public void registersDefaultSimpleTypesWithMappingContext() {
155-
this.context = new AnnotationConfigApplicationContext();
156-
this.context.register(MongoAutoConfiguration.class,
157-
MongoDataAutoConfiguration.class);
158-
this.context.refresh();
159-
MongoMappingContext context = this.context.getBean(MongoMappingContext.class);
160-
BasicMongoPersistentEntity<?> entity = context.getPersistentEntity(Sample.class);
161-
MongoPersistentProperty dateProperty = entity.getPersistentProperty("date");
162-
assertThat(dateProperty.isEntity()).isFalse();
163-
}
156+
this.contextRunner.run((context) -> {
157+
MongoMappingContext mappingContext = context
158+
.getBean(MongoMappingContext.class);
159+
BasicMongoPersistentEntity<?> entity = mappingContext
160+
.getPersistentEntity(Sample.class);
161+
MongoPersistentProperty dateProperty = entity.getPersistentProperty("date");
162+
assertThat(dateProperty.isEntity()).isFalse();
163+
});
164164

165-
public void testFieldNamingStrategy(String strategy,
166-
Class<? extends FieldNamingStrategy> expectedType) {
167-
this.context = new AnnotationConfigApplicationContext();
168-
if (strategy != null) {
169-
TestPropertyValues.of("spring.data.mongodb.field-naming-strategy:" + strategy)
170-
.applyTo(this.context);
171-
}
172-
this.context.register(PropertyPlaceholderAutoConfiguration.class,
173-
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class);
174-
this.context.refresh();
175-
MongoMappingContext mappingContext = this.context
176-
.getBean(MongoMappingContext.class);
177-
FieldNamingStrategy fieldNamingStrategy = (FieldNamingStrategy) ReflectionTestUtils
178-
.getField(mappingContext, "fieldNamingStrategy");
179-
assertThat(fieldNamingStrategy.getClass()).isEqualTo(expectedType);
180165
}
181166

182167
@SuppressWarnings({ "unchecked", "rawtypes" })

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoReactiveDataAutoConfigurationTests.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616

1717
package org.springframework.boot.autoconfigure.data.mongo;
1818

19-
import org.junit.After;
2019
import org.junit.Test;
2120

21+
import org.springframework.boot.autoconfigure.AutoConfigurations;
2222
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
2323
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
2424
import org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration;
25-
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
25+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2626
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
2727

2828
import static org.assertj.core.api.Assertions.assertThat;
@@ -34,23 +34,17 @@
3434
*/
3535
public class MongoReactiveDataAutoConfigurationTests {
3636

37-
private AnnotationConfigApplicationContext context;
38-
39-
@After
40-
public void close() {
41-
if (this.context != null) {
42-
this.context.close();
43-
}
44-
}
37+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
38+
.withConfiguration(AutoConfigurations.of(
39+
PropertyPlaceholderAutoConfiguration.class,
40+
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class,
41+
MongoReactiveAutoConfiguration.class,
42+
MongoReactiveDataAutoConfiguration.class));
4543

4644
@Test
4745
public void templateExists() {
48-
this.context = new AnnotationConfigApplicationContext(
49-
PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class,
50-
MongoDataAutoConfiguration.class, MongoReactiveAutoConfiguration.class,
51-
MongoReactiveDataAutoConfiguration.class);
52-
assertThat(this.context.getBeanNamesForType(ReactiveMongoTemplate.class))
53-
.hasSize(1);
46+
this.contextRunner.run((context) -> assertThat(context)
47+
.hasSingleBean(ReactiveMongoTemplate.class));
5448
}
5549

5650
}

0 commit comments

Comments
 (0)