Sunday, 25 November 2012

Testing with embedded mongodb

If you have to use embedded mongodb instance for testing there is a foursquare implementation called Fongo. You will have to install it locally or deploy it into your intranet repository in order to use with maven.

In order to use it with spring-data-mongodb you can use something like


1:  <bean id="mongoOperations" class="ru.sample.dal.utils.ImMemoryMongoFactory" factory-method="provide" scope="prototype"/>
2:  
3:  <bean id="mongoTemplate" class="ru.sample.dal.utils.ImMemoryMongoFactory" factory-method="provide" scope="prototype"/>
4:  
5:  public class ImMemoryMongoFactory {
6:    private static Fongo fongo = new Fongo("name");
7:  
8:    public static MongoOperations provide() {
9:      return new MongoTemplate(fongo.getMongo(), "database");
10:    }
11:  }


and just autore MongoOperations in your custom repository implemenations. Basic implementation of repositories should start using those beans by default too

No comments:

Post a Comment