Commit 915c959a authored by Stephane Nicoll's avatar Stephane Nicoll

Polish contribution

Closes gh-7600
parent 86485208
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -100,7 +100,7 @@ public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurat ...@@ -100,7 +100,7 @@ public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurat
public void mongoSessionStore() { public void mongoSessionStore() {
load(Arrays.asList(EmbeddedMongoAutoConfiguration.class, load(Arrays.asList(EmbeddedMongoAutoConfiguration.class,
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class), MongoAutoConfiguration.class, MongoDataAutoConfiguration.class),
"spring.session.store-type=mongo", "spring.data.mongodb.port=0"); "spring.session.store-type=mongo");
validateSessionRepository(MongoOperationsSessionRepository.class); validateSessionRepository(MongoOperationsSessionRepository.class);
} }
...@@ -108,7 +108,7 @@ public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurat ...@@ -108,7 +108,7 @@ public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurat
public void mongoSessionStoreWithCustomizations() { public void mongoSessionStoreWithCustomizations() {
load(Arrays.asList(EmbeddedMongoAutoConfiguration.class, load(Arrays.asList(EmbeddedMongoAutoConfiguration.class,
MongoAutoConfiguration.class, MongoDataAutoConfiguration.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"); "spring.session.mongo.collection-name=foobar");
MongoOperationsSessionRepository repository = validateSessionRepository( MongoOperationsSessionRepository repository = validateSessionRepository(
MongoOperationsSessionRepository.class); MongoOperationsSessionRepository.class);
......
...@@ -5687,11 +5687,10 @@ A list of the auto-configuration that is enabled by `@JdbcTest` can be ...@@ -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]] [[boot-features-testing-spring-boot-applications-testing-autoconfigured-mongo-test]]
==== Auto-configured Data MongoDB tests ==== Auto-configured Data MongoDB tests
`@DataMongoTest` is similar to `@DataJpaTest` but for Spring Data MongoDB tests. `@DataMongoTest` can be used if you want to test MongoDB applications. By default, it will
By default it will configure an in-memory Mongod if `de.flapdoodle.embed:de.flapdoodle.embed.mongo` configure an in-memory embedded MongoDB (if available), configure a `MongoTemplate`, scan
is available, configure a `MongoTemplate`, scan for `@Document` classes and for `@Document` classes and configure Spring Data MongoDB repositories. Regular
configure Spring Data MongoDB repositories. Regular `@Component` beans will not `@Component` beans will not be loaded into the `ApplicationContext`:
be loaded into the `ApplicationContext`:
[source,java,indent=0] [source,java,indent=0]
---- ----
...@@ -5704,18 +5703,30 @@ be loaded into the `ApplicationContext`: ...@@ -5704,18 +5703,30 @@ be loaded into the `ApplicationContext`:
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@DataMongoTest @DataMongoTest
public class ExampleDataMongoTests { public class ExampleDataMongoTests {
@Autowired @Autowired
private MongoTemplate mongoTemplate; 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] [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) @DataMongoTest(excludeAutoConfiguration = EmbeddedMongoAutoConfiguration.class)
public class ExampleDataMongoNonEmbeddedTests {
}
---- ----
A list of the auto-configuration that is enabled by `@DataMongoTest` can be A list of the auto-configuration that is enabled by `@DataMongoTest` can be
......
...@@ -22,8 +22,8 @@ import org.springframework.data.mongodb.repository.MongoRepository; ...@@ -22,8 +22,8 @@ import org.springframework.data.mongodb.repository.MongoRepository;
public interface CustomerRepository extends MongoRepository<Customer, String> { public interface CustomerRepository extends MongoRepository<Customer, String> {
public Customer findByFirstName(String firstName); Customer findByFirstName(String firstName);
public List<Customer> findByLastName(String lastName); List<Customer> findByLastName(String lastName);
} }
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -26,11 +26,12 @@ import java.lang.annotation.Target; ...@@ -26,11 +26,12 @@ import java.lang.annotation.Target;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
/** /**
* {@link ImportAutoConfiguration Auto-configuration imports} for typical Data Mongo tests. * {@link ImportAutoConfiguration Auto-configuration imports} for typical Data MongoDB
* Most tests should consider using {@link DataMongoTest @DataMongoTest} rather than using * tests. Most tests should consider using {@link DataMongoTest @DataMongoTest} rather
* this annotation directly. * than using this annotation directly.
* *
* @author Michael J. Simons * @author Michael J. Simons
* @since 1.5.0
* @see DataMongoTest * @see DataMongoTest
*/ */
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -35,17 +35,17 @@ import org.springframework.test.context.BootstrapWith; ...@@ -35,17 +35,17 @@ import org.springframework.test.context.BootstrapWith;
/** /**
* Annotation that can be used in combination with {@code @RunWith(SpringRunner.class)} * 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 * for a typical MongoDB test. Can be used when a test focuses
* <strong>only</strong> on Data MongoDB components, i.e. the MongoTemplate and MongoRepositories. * <strong>only</strong> on MongoDB components.
* <p> * <p>
* Using this annotation will disable full auto-configuration and instead apply only * Using this annotation will disable full auto-configuration and instead apply only
* configuration relevant to Spring Data MongoDB tests. * configuration relevant to MongoDB tests.
* <p> * <p>
* By default, tests annotated with {@code @DataMongoTest} will use an embedded in-memory * 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 * MongoDB process (if available).
* classpath and thus replacing the default connection to a MongoDB.
* *
* @author Michael J. Simons * @author Michael J. Simons
* @author Stephane Nicoll
* @since 1.5.0 * @since 1.5.0
*/ */
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
......
...@@ -13,6 +13,13 @@ org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\ ...@@ -13,6 +13,13 @@ org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\ org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration 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 # AutoConfigureJdbc auto-configuration imports
org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureJdbc=\ org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureJdbc=\
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\ org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
...@@ -101,10 +108,3 @@ org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListen ...@@ -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.client.MockRestServiceServerResetTestExecutionListener,\
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener,\ org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener,\
org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener 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
/*
* 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();
}
}
...@@ -66,4 +66,5 @@ public class DataMongoTestIntegrationTests { ...@@ -66,4 +66,5 @@ public class DataMongoTestIntegrationTests {
this.thrown.expect(NoSuchBeanDefinitionException.class); this.thrown.expect(NoSuchBeanDefinitionException.class);
this.applicationContext.getBean(ExampleService.class); this.applicationContext.getBean(ExampleService.class);
} }
} }
...@@ -45,4 +45,5 @@ public class ExampleDocument { ...@@ -45,4 +45,5 @@ public class ExampleDocument {
public void setText(String text) { public void setText(String text) {
this.text = text; this.text = text;
} }
} }
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -35,4 +35,5 @@ public class ExampleService { ...@@ -35,4 +35,5 @@ public class ExampleService {
public boolean hasCollection(final String collectionName) { public boolean hasCollection(final String collectionName) {
return this.mongoTemplate.collectionExists(collectionName); return this.mongoTemplate.collectionExists(collectionName);
} }
} }
# Use random port for embedded Mongod.
spring.data.mongodb.port = 0
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment