eu.michael-simons:datamongotest-autoconfigure-spring-boot

Parent pom providing dependency and plugin management for applications built with Maven

Лицензия

Лицензия

Категории

Категории

Spring Boot Контейнер Микросервисы Данные Auto Библиотеки уровня приложения Code Generators config Configuration
Группа

Группа

eu.michael-simons
Идентификатор

Идентификатор

datamongotest-autoconfigure-spring-boot
Последняя версия

Последняя версия

0.0.2
Дата

Дата

Тип

Тип

jar
Описание

Описание

Parent pom providing dependency and plugin management for applications built with Maven
Организация-разработчик

Организация-разработчик

michael-simons.eu
Система контроля версий

Система контроля версий

https://github.com/michael-simons/datamongotest-autoconfigure-spring-boot

Скачать datamongotest-autoconfigure-spring-boot

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/eu.michael-simons/datamongotest-autoconfigure-spring-boot/ -->
<dependency>
    <groupId>eu.michael-simons</groupId>
    <artifactId>datamongotest-autoconfigure-spring-boot</artifactId>
    <version>0.0.2</version>
</dependency>
// https://jarcasting.com/artifacts/eu.michael-simons/datamongotest-autoconfigure-spring-boot/
implementation 'eu.michael-simons:datamongotest-autoconfigure-spring-boot:0.0.2'
// https://jarcasting.com/artifacts/eu.michael-simons/datamongotest-autoconfigure-spring-boot/
implementation ("eu.michael-simons:datamongotest-autoconfigure-spring-boot:0.0.2")
'eu.michael-simons:datamongotest-autoconfigure-spring-boot:jar:0.0.2'
<dependency org="eu.michael-simons" name="datamongotest-autoconfigure-spring-boot" rev="0.0.2">
  <artifact name="datamongotest-autoconfigure-spring-boot" type="jar" />
</dependency>
@Grapes(
@Grab(group='eu.michael-simons', module='datamongotest-autoconfigure-spring-boot', version='0.0.2')
)
libraryDependencies += "eu.michael-simons" % "datamongotest-autoconfigure-spring-boot" % "0.0.2"
[eu.michael-simons/datamongotest-autoconfigure-spring-boot "0.0.2"]

Зависимости

compile (2)

Идентификатор библиотеки Тип Версия
org.springframework.boot : spring-boot-starter-data-mongodb jar 1.4.2.RELEASE
org.springframework.boot : spring-boot-starter-test jar 1.4.2.RELEASE

test (1)

Идентификатор библиотеки Тип Версия
de.flapdoodle.embed : de.flapdoodle.embed.mongo jar 1.50.5

Модули Проекта

Данный проект не имеет модулей.

IMPORTANT

You don't need this for Spring Boot >= 1.5.x, it's included now in Spring Boot itself, see: 41.3.10 Auto-configured Data MongoDB tests


datamongotest-autoconfigure-spring-boot

Provides a @DataMongoTest for the automatic configuration of tests with Spring Boot 1.4+.

Note: I have proposed this as a PR for Spring Boot itself: Provide a @DataMongoTest similar to @DataJpaTest

Introduction

Spring Boot 1.4 brought a new feature called Auto-configured tests.

Thoses "slices" can be used when starting a full application auto-configuration is overkill for a specific tests.

The reference documentation has chapter on how to use them.

Stéphane Nicoll created a nice post on how to create your own slice, called Custom test slice with Spring Boot 1.4.

@DataMongoTest uses his work to provide a custom test slice that works with pretty much the same way for Spring Data MongoDB as @DataJpaTest does for Spring Data JPA.

How to use it

Add

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>eu.michael-simons</groupId>
    <artifactId>datamongotest-autoconfigure-spring-boot</artifactId>
    <version>0.0.1</version>
    <scope>test</scope>
</dependency>

to your dependencies, create and write your MongoDB repositories in your Spring Boot Application as you did before.

Annotate your test with @DataMongoTest and benefit from a MongoTemplate and all your repositories:

@RunWith(SpringRunner.class)
@DataMongoTest
public class DataMongoSampleTests {

    @Autowired
    private MongoTemplate mongoTemplate;
    
    @Autowired
    private TweetRepository tweetRepository;

    @Test
    public void testStuff() {
        TweetDocument tweet = new TweetDocument();
        tweet.setText("Look, new @DataMongoTest!");
        
        tweet = this.tweetRepository.save(tweet);
        assertThat(tweet.getId(), notNullValue());
        
        assertTrue(this.mongoTemplate.collectionExists("tweets"));
    }
}

The automatic configuration takes your application.properties into account. Make sure you configure another database connection for your test profile through

spring.data.mongodb.database = testdatabase

Or you might consider adding

<dependency>
    <groupId>de.flapdoodle.embed</groupId>
    <artifactId>de.flapdoodle.embed.mongo</artifactId>
    <scope>test</scope>
</dependency>

to your test dependencies. This activates Spring Boot support for an embedded MongoDB process and you end up with an embedded Mongo connection like you do when using @DataJpaTest where you get an H2 embedded database.

If you don't want this and but have embedded Mongo on your path, consider adding

@AutoConfigureEmbeddedTestMongod(enabled = false)

to your test as well.

Версии библиотеки

Версия
0.0.2
0.0.1