Migrate tests to ApplicationContextRunner
This commit is contained in:
parent
cced3514a7
commit
c8c32cfa33
|
@ -21,17 +21,17 @@ import java.util.Arrays;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import com.mongodb.MongoClient;
|
import com.mongodb.MongoClient;
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.beans.factory.BeanCreationException;
|
import org.springframework.beans.factory.BeanCreationException;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
|
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.data.mongo.city.City;
|
import org.springframework.boot.autoconfigure.data.mongo.city.City;
|
||||||
import org.springframework.boot.autoconfigure.data.mongo.country.Country;
|
import org.springframework.boot.autoconfigure.data.mongo.country.Country;
|
||||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||||
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
|
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
|
||||||
import org.springframework.boot.test.util.TestPropertyValues;
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
@ -48,7 +48,6 @@ import org.springframework.data.mongodb.gridfs.GridFsTemplate;
|
||||||
import org.springframework.test.util.ReflectionTestUtils;
|
import org.springframework.test.util.ReflectionTestUtils;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link MongoDataAutoConfiguration}.
|
* Tests for {@link MongoDataAutoConfiguration}.
|
||||||
|
@ -58,125 +57,111 @@ import static org.junit.Assert.fail;
|
||||||
*/
|
*/
|
||||||
public class MongoDataAutoConfigurationTests {
|
public class MongoDataAutoConfigurationTests {
|
||||||
|
|
||||||
private AnnotationConfigApplicationContext context;
|
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||||
|
.withConfiguration(AutoConfigurations.of(
|
||||||
@After
|
PropertyPlaceholderAutoConfiguration.class,
|
||||||
public void close() {
|
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class));
|
||||||
if (this.context != null) {
|
|
||||||
this.context.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void templateExists() {
|
public void templateExists() {
|
||||||
this.context = new AnnotationConfigApplicationContext(
|
this.contextRunner
|
||||||
PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class,
|
.run((context) -> assertThat(context).hasSingleBean(MongoTemplate.class));
|
||||||
MongoDataAutoConfiguration.class);
|
|
||||||
assertThat(this.context.getBeanNamesForType(MongoTemplate.class).length)
|
|
||||||
.isEqualTo(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void gridFsTemplateExists() {
|
public void gridFsTemplateExists() {
|
||||||
this.context = new AnnotationConfigApplicationContext();
|
this.contextRunner.withPropertyValues("spring.data.mongodb.gridFsDatabase:grid")
|
||||||
TestPropertyValues.of("spring.data.mongodb.gridFsDatabase:grid")
|
.run((context) -> assertThat(context)
|
||||||
.applyTo(this.context);
|
.hasSingleBean(GridFsTemplate.class));
|
||||||
this.context.register(PropertyPlaceholderAutoConfiguration.class,
|
|
||||||
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
assertThat(this.context.getBeanNamesForType(GridFsTemplate.class).length)
|
|
||||||
.isEqualTo(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void customConversions() {
|
public void customConversions() {
|
||||||
this.context = new AnnotationConfigApplicationContext();
|
this.contextRunner.withUserConfiguration(CustomConversionsConfig.class)
|
||||||
this.context.register(CustomConversionsConfig.class);
|
.run((context) -> {
|
||||||
this.context.register(PropertyPlaceholderAutoConfiguration.class,
|
MongoTemplate template = context.getBean(MongoTemplate.class);
|
||||||
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class);
|
assertThat(template.getConverter().getConversionService()
|
||||||
this.context.refresh();
|
.canConvert(MongoClient.class, Boolean.class)).isTrue();
|
||||||
MongoTemplate template = this.context.getBean(MongoTemplate.class);
|
});
|
||||||
assertThat(template.getConverter().getConversionService()
|
|
||||||
.canConvert(MongoClient.class, Boolean.class)).isTrue();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void usesAutoConfigurationPackageToPickUpDocumentTypes() {
|
public void usesAutoConfigurationPackageToPickUpDocumentTypes() {
|
||||||
this.context = new AnnotationConfigApplicationContext();
|
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||||
String cityPackage = City.class.getPackage().getName();
|
String cityPackage = City.class.getPackage().getName();
|
||||||
AutoConfigurationPackages.register(this.context, cityPackage);
|
AutoConfigurationPackages.register(context, cityPackage);
|
||||||
this.context.register(MongoAutoConfiguration.class,
|
context.register(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class);
|
||||||
MongoDataAutoConfiguration.class);
|
try {
|
||||||
this.context.refresh();
|
context.refresh();
|
||||||
assertDomainTypesDiscovered(this.context.getBean(MongoMappingContext.class),
|
assertDomainTypesDiscovered(context.getBean(MongoMappingContext.class),
|
||||||
City.class);
|
City.class);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
context.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void defaultFieldNamingStrategy() {
|
public void defaultFieldNamingStrategy() {
|
||||||
testFieldNamingStrategy(null, PropertyNameFieldNamingStrategy.class);
|
this.contextRunner.run((context) -> {
|
||||||
|
MongoMappingContext mappingContext = context
|
||||||
|
.getBean(MongoMappingContext.class);
|
||||||
|
FieldNamingStrategy fieldNamingStrategy = (FieldNamingStrategy) ReflectionTestUtils
|
||||||
|
.getField(mappingContext, "fieldNamingStrategy");
|
||||||
|
assertThat(fieldNamingStrategy.getClass())
|
||||||
|
.isEqualTo(PropertyNameFieldNamingStrategy.class);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void customFieldNamingStrategy() {
|
public void customFieldNamingStrategy() {
|
||||||
testFieldNamingStrategy(CamelCaseAbbreviatingFieldNamingStrategy.class.getName(),
|
this.contextRunner
|
||||||
CamelCaseAbbreviatingFieldNamingStrategy.class);
|
.withPropertyValues("spring.data.mongodb.field-naming-strategy:"
|
||||||
|
+ CamelCaseAbbreviatingFieldNamingStrategy.class.getName())
|
||||||
|
.run((context) -> {
|
||||||
|
MongoMappingContext mappingContext = context
|
||||||
|
.getBean(MongoMappingContext.class);
|
||||||
|
FieldNamingStrategy fieldNamingStrategy = (FieldNamingStrategy) ReflectionTestUtils
|
||||||
|
.getField(mappingContext, "fieldNamingStrategy");
|
||||||
|
assertThat(fieldNamingStrategy.getClass())
|
||||||
|
.isEqualTo(CamelCaseAbbreviatingFieldNamingStrategy.class);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void interfaceFieldNamingStrategy() {
|
public void interfaceFieldNamingStrategy() {
|
||||||
try {
|
this.contextRunner
|
||||||
testFieldNamingStrategy(FieldNamingStrategy.class.getName(), null);
|
.withPropertyValues("spring.data.mongodb.field-naming-strategy:"
|
||||||
fail("Create FieldNamingStrategy interface should fail");
|
+ FieldNamingStrategy.class.getName())
|
||||||
}
|
.run((context) -> assertThat(context).getFailure()
|
||||||
// We seem to have an inconsistent exception, accept either
|
.isInstanceOf(BeanCreationException.class));
|
||||||
catch (BeanCreationException ex) {
|
|
||||||
// Expected
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public void entityScanShouldSetInitialEntitySet() {
|
public void entityScanShouldSetInitialEntitySet() {
|
||||||
this.context = new AnnotationConfigApplicationContext();
|
this.contextRunner.withUserConfiguration(EntityScanConfig.class)
|
||||||
this.context.register(EntityScanConfig.class,
|
.run((context) -> {
|
||||||
PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class,
|
MongoMappingContext mappingContext = context
|
||||||
MongoDataAutoConfiguration.class);
|
.getBean(MongoMappingContext.class);
|
||||||
this.context.refresh();
|
Set<Class<?>> initialEntitySet = (Set<Class<?>>) ReflectionTestUtils
|
||||||
MongoMappingContext mappingContext = this.context
|
.getField(mappingContext, "initialEntitySet");
|
||||||
.getBean(MongoMappingContext.class);
|
assertThat(initialEntitySet).containsOnly(City.class, Country.class);
|
||||||
Set<Class<?>> initialEntitySet = (Set<Class<?>>) ReflectionTestUtils
|
});
|
||||||
.getField(mappingContext, "initialEntitySet");
|
|
||||||
assertThat(initialEntitySet).containsOnly(City.class, Country.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void registersDefaultSimpleTypesWithMappingContext() {
|
public void registersDefaultSimpleTypesWithMappingContext() {
|
||||||
this.context = new AnnotationConfigApplicationContext();
|
this.contextRunner.run((context) -> {
|
||||||
this.context.register(MongoAutoConfiguration.class,
|
MongoMappingContext mappingContext = context
|
||||||
MongoDataAutoConfiguration.class);
|
.getBean(MongoMappingContext.class);
|
||||||
this.context.refresh();
|
BasicMongoPersistentEntity<?> entity = mappingContext
|
||||||
MongoMappingContext context = this.context.getBean(MongoMappingContext.class);
|
.getPersistentEntity(Sample.class);
|
||||||
BasicMongoPersistentEntity<?> entity = context.getPersistentEntity(Sample.class);
|
MongoPersistentProperty dateProperty = entity.getPersistentProperty("date");
|
||||||
MongoPersistentProperty dateProperty = entity.getPersistentProperty("date");
|
assertThat(dateProperty.isEntity()).isFalse();
|
||||||
assertThat(dateProperty.isEntity()).isFalse();
|
});
|
||||||
}
|
|
||||||
|
|
||||||
public void testFieldNamingStrategy(String strategy,
|
|
||||||
Class<? extends FieldNamingStrategy> expectedType) {
|
|
||||||
this.context = new AnnotationConfigApplicationContext();
|
|
||||||
if (strategy != null) {
|
|
||||||
TestPropertyValues.of("spring.data.mongodb.field-naming-strategy:" + strategy)
|
|
||||||
.applyTo(this.context);
|
|
||||||
}
|
|
||||||
this.context.register(PropertyPlaceholderAutoConfiguration.class,
|
|
||||||
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
MongoMappingContext mappingContext = this.context
|
|
||||||
.getBean(MongoMappingContext.class);
|
|
||||||
FieldNamingStrategy fieldNamingStrategy = (FieldNamingStrategy) ReflectionTestUtils
|
|
||||||
.getField(mappingContext, "fieldNamingStrategy");
|
|
||||||
assertThat(fieldNamingStrategy.getClass()).isEqualTo(expectedType);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||||
|
|
|
@ -16,13 +16,13 @@
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.data.mongo;
|
package org.springframework.boot.autoconfigure.data.mongo;
|
||||||
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
|
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration;
|
import org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
|
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
@ -34,23 +34,17 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
*/
|
*/
|
||||||
public class MongoReactiveDataAutoConfigurationTests {
|
public class MongoReactiveDataAutoConfigurationTests {
|
||||||
|
|
||||||
private AnnotationConfigApplicationContext context;
|
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||||
|
.withConfiguration(AutoConfigurations.of(
|
||||||
@After
|
PropertyPlaceholderAutoConfiguration.class,
|
||||||
public void close() {
|
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class,
|
||||||
if (this.context != null) {
|
MongoReactiveAutoConfiguration.class,
|
||||||
this.context.close();
|
MongoReactiveDataAutoConfiguration.class));
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void templateExists() {
|
public void templateExists() {
|
||||||
this.context = new AnnotationConfigApplicationContext(
|
this.contextRunner.run((context) -> assertThat(context)
|
||||||
PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class,
|
.hasSingleBean(ReactiveMongoTemplate.class));
|
||||||
MongoDataAutoConfiguration.class, MongoReactiveAutoConfiguration.class,
|
|
||||||
MongoReactiveDataAutoConfiguration.class);
|
|
||||||
assertThat(this.context.getBeanNamesForType(ReactiveMongoTemplate.class))
|
|
||||||
.hasSize(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue