From b19d31e0671011d8a97c94cb4572ea14ed87eb79 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 20 Jan 2017 12:08:32 +0100 Subject: [PATCH 1/4] Use a random port with embedded Mongo by default This commit improves the logic of the embedded Mongo support to use a random port if no custom port has been specified. This doesn't change the default if the embedded support isn't active. Closes gh-8044 --- .../EmbeddedMongoAutoConfiguration.java | 22 ++-- .../EmbeddedMongoAutoConfigurationTests.java | 100 +++++++++--------- .../main/asciidoc/spring-boot-features.adoc | 3 + 3 files changed, 63 insertions(+), 62 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java index 7abad3309d..3f8665fcab 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -106,8 +106,9 @@ public class EmbeddedMongoAutoConfiguration { @ConditionalOnMissingBean public MongodExecutable embeddedMongoServer(IMongodConfig mongodConfig) throws IOException { - if (getPort() == 0) { - publishPortInfo(mongodConfig.net().getPort()); + Integer configuredPort = this.properties.getPort(); + if (configuredPort == null || configuredPort == 0) { + setEmbeddedPort(mongodConfig.net().getPort()); } MongodStarter mongodStarter = getMongodStarter(this.runtimeConfig); return mongodStarter.prepare(mongodConfig); @@ -136,8 +137,9 @@ public class EmbeddedMongoAutoConfiguration { ? this.embeddedProperties.getStorage().getOplogSize() : 0)); } - if (getPort() > 0) { - builder.net(new Net(getHost().getHostAddress(), getPort(), + Integer configuredPort = this.properties.getPort(); + if (configuredPort != null && configuredPort > 0) { + builder.net(new Net(getHost().getHostAddress(), configuredPort, Network.localhostIsIPv6())); } else { @@ -147,13 +149,6 @@ public class EmbeddedMongoAutoConfiguration { return builder.build(); } - private int getPort() { - if (this.properties.getPort() == null) { - return MongoProperties.DEFAULT_PORT; - } - return this.properties.getPort(); - } - private InetAddress getHost() throws UnknownHostException { if (this.properties.getHost() == null) { return InetAddress.getByAddress(Network.localhostIsIPv6() @@ -162,7 +157,8 @@ public class EmbeddedMongoAutoConfiguration { return InetAddress.getByName(this.properties.getHost()); } - private void publishPortInfo(int port) { + private void setEmbeddedPort(int port) { + this.properties.setPort(port); setPortProperty(this.context, port); } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java index 18542612f2..f1a069d24b 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -47,6 +47,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Henryk Konsek * @author Andy Wilkinson + * @author Stephane Nicoll */ public class EmbeddedMongoAutoConfigurationTests { @@ -71,28 +72,38 @@ public class EmbeddedMongoAutoConfigurationTests { @Test public void customFeatures() { - this.context = new AnnotationConfigApplicationContext(); - int mongoPort = SocketUtils.findAvailableTcpPort(); - EnvironmentTestUtils.addEnvironment(this.context, - "spring.data.mongodb.port=" + mongoPort, - "spring.mongodb.embedded.features=TEXT_SEARCH, SYNC_DELAY"); - this.context.register(EmbeddedMongoAutoConfiguration.class); - this.context.refresh(); + load("spring.mongodb.embedded.features=TEXT_SEARCH, SYNC_DELAY"); assertThat(this.context.getBean(EmbeddedMongoProperties.class).getFeatures()) .contains(Feature.TEXT_SEARCH, Feature.SYNC_DELAY); } + @Test + public void useRandomPortByDefault() { + load(); + assertThat(this.context.getBeansOfType(MongoClient.class)).hasSize(1); + MongoClient client = this.context.getBean(MongoClient.class); + Integer mongoPort = Integer.valueOf( + this.context.getEnvironment().getProperty("local.mongo.port")); + assertThat(client.getAddress().getPort()).isEqualTo(mongoPort); + } + + @Test + public void specifyPortToZeroAllocateRandomPort() { + load("spring.data.mongodb.port=0"); + assertThat(this.context.getBeansOfType(MongoClient.class)).hasSize(1); + MongoClient client = this.context.getBean(MongoClient.class); + Integer mongoPort = Integer.valueOf( + this.context.getEnvironment().getProperty("local.mongo.port")); + assertThat(client.getAddress().getPort()).isEqualTo(mongoPort); + } + @Test public void randomlyAllocatedPortIsAvailableWhenCreatingMongoClient() { - this.context = new AnnotationConfigApplicationContext(); - EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.port=0"); - this.context.register(EmbeddedMongoAutoConfiguration.class, - MongoClientConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); - assertThat(this.context.getBean(MongoClient.class).getAddress().getPort()) - .isEqualTo(Integer.valueOf( - this.context.getEnvironment().getProperty("local.mongo.port"))); + load(MongoClientConfiguration.class); + MongoClient client = this.context.getBean(MongoClient.class); + Integer mongoPort = Integer.valueOf( + this.context.getEnvironment().getProperty("local.mongo.port")); + assertThat(client.getAddress().getPort()).isEqualTo(mongoPort); } @Test @@ -102,11 +113,8 @@ public class EmbeddedMongoAutoConfigurationTests { try { this.context = new AnnotationConfigApplicationContext(); this.context.setParent(parent); - EnvironmentTestUtils.addEnvironment(this.context, - "spring.data.mongodb.port=0"); this.context.register(EmbeddedMongoAutoConfiguration.class, - MongoClientConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); + MongoClientConfiguration.class); this.context.refresh(); assertThat(parent.getEnvironment().getProperty("local.mongo.port")) .isNotNull(); @@ -118,12 +126,7 @@ public class EmbeddedMongoAutoConfigurationTests { @Test public void defaultStorageConfiguration() { - this.context = new AnnotationConfigApplicationContext(); - EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.port=0"); - this.context.register(EmbeddedMongoAutoConfiguration.class, - MongoClientConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load(MongoClientConfiguration.class); Storage replication = this.context.getBean(IMongodConfig.class).replication(); assertThat(replication.getOplogSize()).isEqualTo(0); assertThat(replication.getDatabaseDir()).isNull(); @@ -134,40 +137,22 @@ public class EmbeddedMongoAutoConfigurationTests { public void mongoWritesToCustomDatabaseDir() { File customDatabaseDir = new File("target/custom-database-dir"); FileSystemUtils.deleteRecursively(customDatabaseDir); - this.context = new AnnotationConfigApplicationContext(); - EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.port=0", - "spring.mongodb.embedded.storage.databaseDir=" - + customDatabaseDir.getPath()); - this.context.register(EmbeddedMongoAutoConfiguration.class, - MongoClientConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load("spring.mongodb.embedded.storage.databaseDir=" + + customDatabaseDir.getPath()); assertThat(customDatabaseDir).isDirectory(); assertThat(customDatabaseDir.listFiles()).isNotEmpty(); } @Test public void customOpLogSizeIsAppliedToConfiguration() { - this.context = new AnnotationConfigApplicationContext(); - EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.port=0", - "spring.mongodb.embedded.storage.oplogSize=10"); - this.context.register(EmbeddedMongoAutoConfiguration.class, - MongoClientConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load("spring.mongodb.embedded.storage.oplogSize=10"); assertThat(this.context.getBean(IMongodConfig.class).replication().getOplogSize()) .isEqualTo(10); } @Test public void customReplicaSetNameIsAppliedToConfiguration() { - this.context = new AnnotationConfigApplicationContext(); - EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.port=0", - "spring.mongodb.embedded.storage.replSetName=testing"); - this.context.register(EmbeddedMongoAutoConfiguration.class, - MongoClientConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - this.context.refresh(); + load("spring.mongodb.embedded.storage.replSetName=testing"); assertThat( this.context.getBean(IMongodConfig.class).replication().getReplSetName()) .isEqualTo("testing"); @@ -192,6 +177,23 @@ public class EmbeddedMongoAutoConfigurationTests { assertThat(buildInfo.getString("version")).isEqualTo(expectedVersion); } + private void load(String... environment) { + load(null, environment); + } + + private void load(Class config, String... environment) { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); + if (config != null) { + ctx.register(config); + } + EnvironmentTestUtils.addEnvironment(ctx, environment); + ctx.register(EmbeddedMongoAutoConfiguration.class, + MongoAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + ctx.refresh(); + this.context = ctx; + } + @Configuration static class MongoClientConfiguration { diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 821d0995d3..696c24ee88 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -3446,6 +3446,9 @@ property. To use a randomly allocated free port use a value of zero. The `MongoC created by `MongoAutoConfiguration` will be automatically configured to use the randomly allocated port. +NOTE: If you do not configure a custom port, the embedded support will use a random port +by default (rather than 27017). + If you have SLF4J on the classpath, output produced by Mongo will be automatically routed to a logger named `org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongo`. From 8648520876425a37f6983dbeb63135e0d2770e99 Mon Sep 17 00:00:00 2001 From: "Michael J. Simons" Date: Thu, 8 Dec 2016 12:13:54 +0100 Subject: [PATCH 2/4] Add slide test annotation for MongoDB This commits adds a slice test infrastructure for MongoDB, similar to what `@DataJpaTest` does with JPA. By default, an embedded Mongod process is used if available. See gh-7600 --- spring-boot-docs/src/main/asciidoc/index.adoc | 2 +- .../main/asciidoc/spring-boot-features.adoc | 38 ++++++++ spring-boot-test-autoconfigure/pom.xml | 10 ++ .../data/mongo/AutoConfigureDataMongo.java | 42 ++++++++ .../data/mongo/DataMongoTest.java | 96 +++++++++++++++++++ .../mongo/DataMongoTypeExcludeFilter.java | 77 +++++++++++++++ .../main/resources/META-INF/spring.factories | 7 ++ ...DataMongoTestDisableEmbeddedMongoTest.java | 48 ++++++++++ .../mongo/DataMongoTestIntegrationTests.java | 69 +++++++++++++ ...TestWithIncludeFilterIntegrationTests.java | 46 +++++++++ .../data/mongo/ExampleDocument.java | 48 ++++++++++ .../data/mongo/ExampleMongoApplication.java | 29 ++++++ .../data/mongo/ExampleRepository.java | 27 ++++++ .../data/mongo/ExampleService.java | 38 ++++++++ .../src/test/resources/application.properties | 2 + 15 files changed, 578 insertions(+), 1 deletion(-) create mode 100644 spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/AutoConfigureDataMongo.java create mode 100644 spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTest.java create mode 100644 spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTypeExcludeFilter.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestDisableEmbeddedMongoTest.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestIntegrationTests.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestWithIncludeFilterIntegrationTests.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleDocument.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleMongoApplication.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleRepository.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleService.java create mode 100644 spring-boot-test-autoconfigure/src/test/resources/application.properties diff --git a/spring-boot-docs/src/main/asciidoc/index.adoc b/spring-boot-docs/src/main/asciidoc/index.adoc index a60242f89b..3e5376cf26 100644 --- a/spring-boot-docs/src/main/asciidoc/index.adoc +++ b/spring-boot-docs/src/main/asciidoc/index.adoc @@ -1,5 +1,5 @@ = Spring Boot Reference Guide -Phillip Webb; Dave Syer; Josh Long; Stéphane Nicoll; Rob Winch; Andy Wilkinson; Marcel Overdijk; Christian Dupuis; Sébastien Deleuze +Phillip Webb; Dave Syer; Josh Long; Stéphane Nicoll; Rob Winch; Andy Wilkinson; Marcel Overdijk; Christian Dupuis; Sébastien Deleuze; Michael Simons :doctype: book :toc: :toclevels: 4 diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 696c24ee88..cfab290456 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -5685,6 +5685,44 @@ A list of the auto-configuration that is enabled by `@JdbcTest` can be +[[boot-features-testing-spring-boot-applications-testing-autoconfigured-mongo-test]] +==== Auto-configured Data MongoDB tests +`@DataMongoTest` is similar to `@DataJpaTest` but for Spring Data MongoDB tests. +By default it will configure an in-memory Mongod if `de.flapdoodle.embed:de.flapdoodle.embed.mongo` +is available, configure a `MongoTemplate`, scan for `@Document` classes and +configure Spring Data MongoDB repositories. Regular `@Component` beans will not +be loaded into the `ApplicationContext`: + +[source,java,indent=0] +---- + import org.junit.runner.RunWith; + import org.springframework.beans.factory.annotation.Autowired; + import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; + import org.springframework.data.mongodb.core.MongoTemplate; + import org.springframework.test.context.junit4.SpringRunner; + + @RunWith(SpringRunner.class) + @DataMongoTest + public class ExampleDataMongoTests { + @Autowired + private MongoTemplate mongoTemplate; + + // ...Your autowired Mongo repositories + } +---- + +The If you want to use `@DataMongoTest` but not the embedded Mongod even if you have `de.flapdoodle.embed:de.flapdoodle.embed.mongo` on the class path, exclude its auto-configuration like so: + +[source,java,indent=0] +---- + @DataMongoTest(excludeAutoConfiguration = EmbeddedMongoAutoConfiguration.class) +---- + +A list of the auto-configuration that is enabled by `@DataMongoTest` can be +<>. + + + [[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-client]] ==== Auto-configured REST clients The `@RestClientTest` annotation can be used if you want to test REST clients. By default diff --git a/spring-boot-test-autoconfigure/pom.xml b/spring-boot-test-autoconfigure/pom.xml index d894c5be02..31af1b998e 100644 --- a/spring-boot-test-autoconfigure/pom.xml +++ b/spring-boot-test-autoconfigure/pom.xml @@ -106,6 +106,11 @@ true + + org.springframework.data + spring-data-mongodb + true + org.springframework.restdocs spring-restdocs-mockmvc @@ -177,5 +182,10 @@ tomcat-embed-el test + + de.flapdoodle.embed + de.flapdoodle.embed.mongo + test + diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/AutoConfigureDataMongo.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/AutoConfigureDataMongo.java new file mode 100644 index 0000000000..3289545659 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/AutoConfigureDataMongo.java @@ -0,0 +1,42 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.mongo; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; + +/** + * {@link ImportAutoConfiguration Auto-configuration imports} for typical Data Mongo tests. + * Most tests should consider using {@link DataMongoTest @DataMongoTest} rather than using + * this annotation directly. + * + * @author Michael J. Simons + * @see DataMongoTest + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@ImportAutoConfiguration +public @interface AutoConfigureDataMongo { +} diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTest.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTest.java new file mode 100644 index 0000000000..dfc951d408 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTest.java @@ -0,0 +1,96 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.mongo; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration; +import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache; +import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters; +import org.springframework.boot.test.context.SpringBootTestContextBootstrapper; +import org.springframework.context.annotation.ComponentScan.Filter; +import org.springframework.core.annotation.AliasFor; +import org.springframework.test.context.BootstrapWith; + +/** + * Annotation that can be used in combination with {@code @RunWith(SpringRunner.class)} + * for a typical Spring Data MongoDB test. Can be used when a test focuses + * only on Data MongoDB components, i.e. the MongoTemplate and MongoRepositories. + *

+ * Using this annotation will disable full auto-configuration and instead apply only + * configuration relevant to Spring Data MongoDB tests. + *

+ * By default, tests annotated with {@code @DataMongoTest} will use an embedded in-memory + * Mongod process if "de.flapdoodle.embed:de.flapdoodle.embed.mongo" is on the + * classpath and thus replacing the default connection to a MongoDB. + * + * @author Michael J. Simons + * @since 1.5.0 + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@BootstrapWith(SpringBootTestContextBootstrapper.class) +@OverrideAutoConfiguration(enabled = false) +@TypeExcludeFilters(DataMongoTypeExcludeFilter.class) +@AutoConfigureCache +@AutoConfigureDataMongo +@ImportAutoConfiguration +public @interface DataMongoTest { + + /** + * Determines if default filtering should be used with + * {@link SpringBootApplication @SpringBootApplication}. By default no beans + * are included. + * + * @see #includeFilters() + * @see #excludeFilters() + * @return if default filters should be used + */ + boolean useDefaultFilters() default true; + + /** + * A set of include filters which can be used to add otherwise filtered + * beans to the application context. + * + * @return include filters to apply + */ + Filter[] includeFilters() default {}; + + /** + * A set of exclude filters which can be used to filter beans that would + * otherwise be added to the application context. + * + * @return exclude filters to apply + */ + Filter[] excludeFilters() default {}; + + /** + * Auto-configuration exclusions that should be applied for this test. + * @return auto-configuration exclusions to apply + */ + @AliasFor(annotation = ImportAutoConfiguration.class, attribute = "exclude") + Class[] excludeAutoConfiguration() default {}; +} diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTypeExcludeFilter.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTypeExcludeFilter.java new file mode 100644 index 0000000000..65e0afd26b --- /dev/null +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTypeExcludeFilter.java @@ -0,0 +1,77 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.mongo; + +import java.io.IOException; +import java.util.Collections; +import java.util.Set; + +import org.springframework.boot.context.TypeExcludeFilter; +import org.springframework.boot.test.autoconfigure.filter.AnnotationCustomizableTypeExcludeFilter; +import org.springframework.context.annotation.ComponentScan.Filter; +import org.springframework.core.annotation.AnnotatedElementUtils; +import org.springframework.core.type.classreading.MetadataReader; +import org.springframework.core.type.classreading.MetadataReaderFactory; + +/** + * {@link TypeExcludeFilter} for {@link DataMongoTest @DataMongoTest}. + * + * @author Michael J. Simons + */ +class DataMongoTypeExcludeFilter extends AnnotationCustomizableTypeExcludeFilter { + + private final DataMongoTest annotation; + + DataMongoTypeExcludeFilter(final Class testClass) { + this.annotation = AnnotatedElementUtils.getMergedAnnotation(testClass, + DataMongoTest.class); + } + + @Override + protected boolean hasAnnotation() { + return this.annotation != null; + } + + @Override + protected Filter[] getFilters(final FilterType type) { + switch (type) { + case INCLUDE: + return this.annotation.includeFilters(); + case EXCLUDE: + return this.annotation.excludeFilters(); + default: + throw new IllegalStateException("Unsupported type " + type); + } + } + + @Override + protected boolean isUseDefaultFilters() { + return this.annotation.useDefaultFilters(); + } + + @Override + protected boolean defaultInclude(final MetadataReader metadataReader, + final MetadataReaderFactory metadataReaderFactory) throws IOException { + return false; + } + + @Override + protected Set> getDefaultIncludes() { + return Collections.emptySet(); + } + +} diff --git a/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories index 70e3788df9..77e9e022b5 100644 --- a/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories @@ -101,3 +101,10 @@ org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListen org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener,\ org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener,\ org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener + +# AutoConfigureDataMongo auto-configuration imports +org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo=\ +org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\ +org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\ +org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\ +org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestDisableEmbeddedMongoTest.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestDisableEmbeddedMongoTest.java new file mode 100644 index 0000000000..46d4807228 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestDisableEmbeddedMongoTest.java @@ -0,0 +1,48 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.mongo; + +import de.flapdoodle.embed.mongo.MongodExecutable; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + + +/** + * @author Michael J. Simons + */ +@RunWith(SpringRunner.class) +@DataMongoTest(excludeAutoConfiguration = EmbeddedMongoAutoConfiguration.class) +public class DataMongoTestDisableEmbeddedMongoTest { + + @Autowired + private ApplicationContext context; + + @Test + public void testContextLoads() throws Exception { + + assertThat(this.context).isNotNull(); + assertThat(this.context.getBeanNamesForType(MongodExecutable.class)).isEmpty(); + } +} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestIntegrationTests.java new file mode 100644 index 0000000000..e87714e857 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestIntegrationTests.java @@ -0,0 +1,69 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.mongo; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Sample test for {@link DataMongoTest @DataMongoTest} + * + * @author Michael J. Simons + */ +@RunWith(SpringRunner.class) +@DataMongoTest +public class DataMongoTestIntegrationTests { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Autowired + private MongoTemplate mongoTemplate; + + @Autowired + private ExampleRepository exampleRepository; + + @Autowired + private ApplicationContext applicationContext; + + @Test + public void testRepository() { + ExampleDocument exampleDocument = new ExampleDocument(); + exampleDocument.setText("Look, new @DataMongoTest!"); + + exampleDocument = this.exampleRepository.save(exampleDocument); + assertThat(exampleDocument.getId()).isNotNull(); + + assertThat(this.mongoTemplate.collectionExists("exampleDocuments")).isTrue(); + } + + @Test + public void didNotInjectExampleController() { + this.thrown.expect(NoSuchBeanDefinitionException.class); + this.applicationContext.getBean(ExampleService.class); + } +} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestWithIncludeFilterIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestWithIncludeFilterIntegrationTests.java new file mode 100644 index 0000000000..53a4e6666b --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestWithIncludeFilterIntegrationTests.java @@ -0,0 +1,46 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.mongo; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.ComponentScan.Filter; +import org.springframework.stereotype.Service; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Integration test with custom include filter for {@link DataMongoTest}. + * + * @author Michael J. Simons + */ +@RunWith(SpringRunner.class) +@DataMongoTest(includeFilters = @Filter(Service.class)) +public class DataMongoTestWithIncludeFilterIntegrationTests { + + @Autowired + private ExampleService service; + + @Test + public void testRepository() { + assertThat(this.service.hasCollection("foobar")).isFalse(); + } + +} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleDocument.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleDocument.java new file mode 100644 index 0000000000..1e2f6be568 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleDocument.java @@ -0,0 +1,48 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.mongo; + +import org.springframework.data.mongodb.core.mapping.Document; + +/** + * Example document used with {@link DataMongoTest} tests. + * + * @author Michael J. Simons + */ +@Document(collection = "exampleDocuments") +public class ExampleDocument { + + private String id; + + private String text; + + public String getId() { + return this.id; + } + + public void setId(String id) { + this.id = id; + } + + public String getText() { + return this.text; + } + + public void setText(String text) { + this.text = text; + } +} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleMongoApplication.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleMongoApplication.java new file mode 100644 index 0000000000..3d6960701f --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleMongoApplication.java @@ -0,0 +1,29 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.mongo; + +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Example {@link SpringBootApplication} used with {@link DataMongoTest} tests. + * + * @author Michael J. Simons + */ +@SpringBootApplication +public class ExampleMongoApplication { + +} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleRepository.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleRepository.java new file mode 100644 index 0000000000..db3337186f --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleRepository.java @@ -0,0 +1,27 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.mongo; + +import org.springframework.data.mongodb.repository.MongoRepository; + +/** + * Example repository used with {@link DataMongoTest} tests. + * + * @author Michael J. Simons + */ +public interface ExampleRepository extends MongoRepository { +} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleService.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleService.java new file mode 100644 index 0000000000..fb24a74188 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleService.java @@ -0,0 +1,38 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.mongo; + +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.stereotype.Service; + +/** + * Example service used with {@link DataMongoTest} tests. + * + * @author Michael J. Simons + */ +@Service +public class ExampleService { + private final MongoTemplate mongoTemplate; + + public ExampleService(MongoTemplate mongoTemplate) { + this.mongoTemplate = mongoTemplate; + } + + public boolean hasCollection(final String collectionName) { + return this.mongoTemplate.collectionExists(collectionName); + } +} diff --git a/spring-boot-test-autoconfigure/src/test/resources/application.properties b/spring-boot-test-autoconfigure/src/test/resources/application.properties new file mode 100644 index 0000000000..4cb7d1fb7c --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/resources/application.properties @@ -0,0 +1,2 @@ +# Use random port for embedded Mongod. +spring.data.mongodb.port = 0 From 915c959a280096b362ff719a69eef629b987c19a Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 16 Jan 2017 13:06:40 +0100 Subject: [PATCH 3/4] Polish contribution Closes gh-7600 --- .../SessionAutoConfigurationTests.java | 6 +-- .../main/asciidoc/spring-boot-features.adoc | 25 +++++++--- .../sample/data/mongo/CustomerRepository.java | 4 +- .../src/main/resources/application.properties | 1 - .../data/mongo/AutoConfigureDataMongo.java | 9 ++-- .../data/mongo/DataMongoTest.java | 12 ++--- .../main/resources/META-INF/spring.factories | 14 +++--- ...DataMongoTestDisableEmbeddedMongoTest.java | 48 ------------------- .../mongo/DataMongoTestIntegrationTests.java | 1 + .../data/mongo/ExampleDocument.java | 1 + .../data/mongo/ExampleService.java | 3 +- .../src/test/resources/application.properties | 2 - 12 files changed, 45 insertions(+), 81 deletions(-) delete mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestDisableEmbeddedMongoTest.java delete mode 100644 spring-boot-test-autoconfigure/src/test/resources/application.properties diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java index dc2bd1a975..f8b3b46b9f 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -100,7 +100,7 @@ public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurat public void mongoSessionStore() { load(Arrays.asList(EmbeddedMongoAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class), - "spring.session.store-type=mongo", "spring.data.mongodb.port=0"); + "spring.session.store-type=mongo"); validateSessionRepository(MongoOperationsSessionRepository.class); } @@ -108,7 +108,7 @@ public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurat public void mongoSessionStoreWithCustomizations() { load(Arrays.asList(EmbeddedMongoAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class), - "spring.session.store-type=mongo", "spring.data.mongodb.port=0", + "spring.session.store-type=mongo", "spring.session.mongo.collection-name=foobar"); MongoOperationsSessionRepository repository = validateSessionRepository( MongoOperationsSessionRepository.class); diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index cfab290456..506c15359d 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -5687,11 +5687,10 @@ A list of the auto-configuration that is enabled by `@JdbcTest` can be [[boot-features-testing-spring-boot-applications-testing-autoconfigured-mongo-test]] ==== Auto-configured Data MongoDB tests -`@DataMongoTest` is similar to `@DataJpaTest` but for Spring Data MongoDB tests. -By default it will configure an in-memory Mongod if `de.flapdoodle.embed:de.flapdoodle.embed.mongo` -is available, configure a `MongoTemplate`, scan for `@Document` classes and -configure Spring Data MongoDB repositories. Regular `@Component` beans will not -be loaded into the `ApplicationContext`: +`@DataMongoTest` can be used if you want to test MongoDB applications. By default, it will +configure an in-memory embedded MongoDB (if available), configure a `MongoTemplate`, scan +for `@Document` classes and configure Spring Data MongoDB repositories. Regular +`@Component` beans will not be loaded into the `ApplicationContext`: [source,java,indent=0] ---- @@ -5704,18 +5703,30 @@ be loaded into the `ApplicationContext`: @RunWith(SpringRunner.class) @DataMongoTest public class ExampleDataMongoTests { + @Autowired private MongoTemplate mongoTemplate; - // ...Your autowired Mongo repositories + // } ---- -The If you want to use `@DataMongoTest` but not the embedded Mongod even if you have `de.flapdoodle.embed:de.flapdoodle.embed.mongo` on the class path, exclude its auto-configuration like so: +In-memory embedded MongoDB generally works well for tests since it is fast and doesn't +require any developer installation. If, however, you prefer to run tests against a real +MongoDB server you should exclude the embedded mongodb auto-configuration: [source,java,indent=0] ---- + import org.junit.runner.RunWith; + import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration; + import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; + import org.springframework.test.context.junit4.SpringRunner; + + @RunWith(SpringRunner.class) @DataMongoTest(excludeAutoConfiguration = EmbeddedMongoAutoConfiguration.class) + public class ExampleDataMongoNonEmbeddedTests { + + } ---- A list of the auto-configuration that is enabled by `@DataMongoTest` can be diff --git a/spring-boot-samples/spring-boot-sample-data-mongodb/src/main/java/sample/data/mongo/CustomerRepository.java b/spring-boot-samples/spring-boot-sample-data-mongodb/src/main/java/sample/data/mongo/CustomerRepository.java index 86602eef98..2243a183c5 100644 --- a/spring-boot-samples/spring-boot-sample-data-mongodb/src/main/java/sample/data/mongo/CustomerRepository.java +++ b/spring-boot-samples/spring-boot-sample-data-mongodb/src/main/java/sample/data/mongo/CustomerRepository.java @@ -22,8 +22,8 @@ import org.springframework.data.mongodb.repository.MongoRepository; public interface CustomerRepository extends MongoRepository { - public Customer findByFirstName(String firstName); + Customer findByFirstName(String firstName); - public List findByLastName(String lastName); + List findByLastName(String lastName); } diff --git a/spring-boot-samples/spring-boot-sample-data-mongodb/src/main/resources/application.properties b/spring-boot-samples/spring-boot-sample-data-mongodb/src/main/resources/application.properties index 03ef29e288..e69de29bb2 100644 --- a/spring-boot-samples/spring-boot-sample-data-mongodb/src/main/resources/application.properties +++ b/spring-boot-samples/spring-boot-sample-data-mongodb/src/main/resources/application.properties @@ -1 +0,0 @@ -spring.data.mongodb.port=0 \ No newline at end of file diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/AutoConfigureDataMongo.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/AutoConfigureDataMongo.java index 3289545659..6db6772dee 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/AutoConfigureDataMongo.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/AutoConfigureDataMongo.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,11 +26,12 @@ import java.lang.annotation.Target; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; /** - * {@link ImportAutoConfiguration Auto-configuration imports} for typical Data Mongo tests. - * Most tests should consider using {@link DataMongoTest @DataMongoTest} rather than using - * this annotation directly. + * {@link ImportAutoConfiguration Auto-configuration imports} for typical Data MongoDB + * tests. Most tests should consider using {@link DataMongoTest @DataMongoTest} rather + * than using this annotation directly. * * @author Michael J. Simons + * @since 1.5.0 * @see DataMongoTest */ @Target(ElementType.TYPE) diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTest.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTest.java index dfc951d408..9aba52c9a9 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTest.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,17 +35,17 @@ import org.springframework.test.context.BootstrapWith; /** * Annotation that can be used in combination with {@code @RunWith(SpringRunner.class)} - * for a typical Spring Data MongoDB test. Can be used when a test focuses - * only on Data MongoDB components, i.e. the MongoTemplate and MongoRepositories. + * for a typical MongoDB test. Can be used when a test focuses + * only on MongoDB components. *

* Using this annotation will disable full auto-configuration and instead apply only - * configuration relevant to Spring Data MongoDB tests. + * configuration relevant to MongoDB tests. *

* By default, tests annotated with {@code @DataMongoTest} will use an embedded in-memory - * Mongod process if "de.flapdoodle.embed:de.flapdoodle.embed.mongo" is on the - * classpath and thus replacing the default connection to a MongoDB. + * MongoDB process (if available). * * @author Michael J. Simons + * @author Stephane Nicoll * @since 1.5.0 */ @Target(ElementType.TYPE) diff --git a/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories index 77e9e022b5..9b55493c20 100644 --- a/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories @@ -13,6 +13,13 @@ org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\ org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\ org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration +# AutoConfigureDataMongo auto-configuration imports +org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo=\ +org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\ +org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\ +org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\ +org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration + # AutoConfigureJdbc auto-configuration imports org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureJdbc=\ org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\ @@ -101,10 +108,3 @@ org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListen org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener,\ org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener,\ org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener - -# AutoConfigureDataMongo auto-configuration imports -org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo=\ -org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\ -org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\ -org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\ -org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestDisableEmbeddedMongoTest.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestDisableEmbeddedMongoTest.java deleted file mode 100644 index 46d4807228..0000000000 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestDisableEmbeddedMongoTest.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2012-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot.test.autoconfigure.data.mongo; - -import de.flapdoodle.embed.mongo.MongodExecutable; - -import org.junit.Test; -import org.junit.runner.RunWith; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration; -import org.springframework.context.ApplicationContext; -import org.springframework.test.context.junit4.SpringRunner; - -import static org.assertj.core.api.Assertions.assertThat; - - -/** - * @author Michael J. Simons - */ -@RunWith(SpringRunner.class) -@DataMongoTest(excludeAutoConfiguration = EmbeddedMongoAutoConfiguration.class) -public class DataMongoTestDisableEmbeddedMongoTest { - - @Autowired - private ApplicationContext context; - - @Test - public void testContextLoads() throws Exception { - - assertThat(this.context).isNotNull(); - assertThat(this.context.getBeanNamesForType(MongodExecutable.class)).isEmpty(); - } -} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestIntegrationTests.java index e87714e857..ee6f7a3fcd 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestIntegrationTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestIntegrationTests.java @@ -66,4 +66,5 @@ public class DataMongoTestIntegrationTests { this.thrown.expect(NoSuchBeanDefinitionException.class); this.applicationContext.getBean(ExampleService.class); } + } diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleDocument.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleDocument.java index 1e2f6be568..1f9d2e8777 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleDocument.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleDocument.java @@ -45,4 +45,5 @@ public class ExampleDocument { public void setText(String text) { this.text = text; } + } diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleService.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleService.java index fb24a74188..c315b5f503 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleService.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleService.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,4 +35,5 @@ public class ExampleService { public boolean hasCollection(final String collectionName) { return this.mongoTemplate.collectionExists(collectionName); } + } diff --git a/spring-boot-test-autoconfigure/src/test/resources/application.properties b/spring-boot-test-autoconfigure/src/test/resources/application.properties deleted file mode 100644 index 4cb7d1fb7c..0000000000 --- a/spring-boot-test-autoconfigure/src/test/resources/application.properties +++ /dev/null @@ -1,2 +0,0 @@ -# Use random port for embedded Mongod. -spring.data.mongodb.port = 0 From 5b57beca57d7c18cad5078b1e1d1895c7058d2b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Fri, 20 Jan 2017 10:33:12 -0500 Subject: [PATCH 4/4] Upgrade to Spring LDAP 2.3.0.RELEASE Closes gh-8047 --- spring-boot-dependencies/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot-dependencies/pom.xml b/spring-boot-dependencies/pom.xml index 463b491ce4..79007d5057 100644 --- a/spring-boot-dependencies/pom.xml +++ b/spring-boot-dependencies/pom.xml @@ -161,7 +161,7 @@ 4.3.6.RELEASE 1.2.1.RELEASE 1.1.2.RELEASE - 2.3.0.BUILD-SNAPSHOT + 2.3.0.RELEASE 1.2.6.RELEASE 1.1.5.RELEASE 1.2.0.RELEASE