Giter VIP home page Giter VIP logo

datamongotest-autoconfigure-spring-boot-deprecated-'s Introduction

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.

datamongotest-autoconfigure-spring-boot-deprecated-'s People

Contributors

michael-simons avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

datamongotest-autoconfigure-spring-boot-deprecated-'s Issues

How do I setup a random port for the embedded mongo instance for this

I noted that when I have mongo running in the background while I used the embedded test, the test will fail with an address in use (port 27017).

Is there a way to set the embedded database to use a different port and have this work?

I can stop my mongo instance and it will work but ...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.