Spring Data MongoDB: Unit tests with repositories

junit4, spring-data, spring-data-mongodb

Solution

Jaranda,

I faced the same problem last week and coincidentally I heard about Fongo, "an in-memory java implementation of mongo."

So I decide to use it to test my custom repositories and worked perfectly to me. Below is an example of how to configure Spring to use Fongo in JUnit tests. Note that I'm not using xml configuration.

Hope that will be useful!

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class LocationRepositoryTest {

   private static final String PLAYER_ID = ObjectId.get().toString();

   @Autowired private LocationRepositoryCustom playerRepository;
   @Autowired private MongoTemplate mongoTemplate;

   /* Some tests... */

   @Configuration
   static class LocationRepositoryTestConfiguration {

        @Bean
        public Mongo mongo() {
            // Configure a Fongo instance
            return new Fongo("mongo-test").getMongo();
        }

        @Bean
        public MongoTemplate mongoTemplate() {
            return new MongoTemplate(mongo(), "collection-name");
        }

        @Bean
        public LocationRepositoryCustom playerRepository() {
            // This is necessary if MongoTemplate is an argument of custom implementation constructor
            return new LocationRepositoryCustomImpl(mongoTemplate());
        }
    }
}

Problem

How is it supposed to build some tests with the repository approach in Spring Data MongoDB? I would like to set the test database for my tests since I don't want to use the production database for this purpose. It should be probably possible but I have no idea. This is my application context: ``` <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xmlns:neo4j="http://www.springframework.org/schema/data/neo4j" xsi:schemaLocation= "http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd"> <!-- Default bean name is 'mongo' --> <mongo:mongo host="${mongo.host}" port="${mongo.port}"> <mongo:options connections-per-host="8" threads-allowed-to-block-for-connection-multiplier="4" connect-timeout="${mongo.connect-timeout}" max-wait-time="${mongo.max-wait-time}" auto-connect-retry="true" socket-keep-alive="true" socket-timeout="${mongo.socket-timeout}" slave-ok="true" write-number="1" write-timeout="0" write-fsync="true"/> </mongo:mongo> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongo" /> <constructor-arg name="databaseName" value="${mongo.db}" /> </bean> <context:component-scan base-package="domain.company.group.project.data.repositories"/> <!-- MongoDB repositories --> <mongo:repositories base-package="domain.company.group.project.data.repositories.mongodb"/> <!-- some other stuff --> </beans> ``` And let's say I have a simple repository as follows: ``` public interface LocationRepository extends MongoRepository<Location, String>, LocationRepositoryCustom { } ``` where LocationRepositoryImpl is the class implementing all my custom methods for a certain Location (domain object) class. My test class looks like: ``` @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"/test-context.xml"}) public class LocationRepositoryTest { @Autowired private LocationRepository locationRepository; /* Some tests... */ } ``` I have tried to embed a MongoDB instance within my running tests (as explained here) but it does not work: the connection to the test database is established but the mongo template seems not able to be overwritten as all save methods keep inserting data to the "production" database. I am using Spring 3.2.0 and Spring Data Mongo 1.1.0.RELEASE. I am using Junit for testing. Any suggestions? Thank you in advance.

Original source