parent
8648520876
commit
915c959a28
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -22,8 +22,8 @@ import org.springframework.data.mongodb.repository.MongoRepository;
|
|||
|
||||
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);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
spring.data.mongodb.port=0
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
* <strong>only</strong> on Data MongoDB components, i.e. the MongoTemplate and MongoRepositories.
|
||||
* for a typical MongoDB test. Can be used when a test focuses
|
||||
* <strong>only</strong> on MongoDB components.
|
||||
* <p>
|
||||
* 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>
|
||||
* 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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -66,4 +66,5 @@ public class DataMongoTestIntegrationTests {
|
|||
this.thrown.expect(NoSuchBeanDefinitionException.class);
|
||||
this.applicationContext.getBean(ExampleService.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,4 +45,5 @@ public class ExampleDocument {
|
|||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,2 +0,0 @@
|
|||
# Use random port for embedded Mongod.
|
||||
spring.data.mongodb.port = 0
|
||||
Loading…
Reference in New Issue