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

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

License

License

Categories

Categories

Spring Boot Container Microservices Data Auto Application Layer Libs Code Generators config Configuration
GroupId

GroupId

eu.michael-simons
ArtifactId

ArtifactId

datamongotest-autoconfigure-spring-boot
Last Version

Last Version

0.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

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

Project Organization

michael-simons.eu
Source Code Management

Source Code Management

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

Download datamongotest-autoconfigure-spring-boot

How to add to project

<!-- 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"]

Dependencies

compile (2)

Group / Artifact Type Version
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)

Group / Artifact Type Version
de.flapdoodle.embed : de.flapdoodle.embed.mongo jar 1.50.5

Project Modules

There are no modules declared in this project.

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.

Versions

Version
0.0.2
0.0.1