beku8
beku8

Reputation: 611

Instantiate Spring MongoRepository manually

I'm trying to test my spring data mongodb repositories, which are interfaces extending from MongoRepository, with embeddedMongoDb. Like this tutorial, I would like to create tests that don't use spring application context, which is achievable if I were using plain mongoTemplate with my repository classes.

So is it possible to instantiate MongoRepository interface implementation by passing Mongo & MongoTemplate instances, using provided utility methods. I assume spring does that automatically on start-up.

Upvotes: 3

Views: 2191

Answers (1)

Mentor Reka
Mentor Reka

Reputation: 9407

As i understand you want to test your application without using xml configuration. Like you tutorial, the configuration of your application context in java class or in xml file is the same finaly.

Personaly for my tests i use Junit and call my xml configuration like this:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"classpath:META-INF/spring/applicationContext-mongo.xml"})
    public class LicenseImplTest extends AbstractJUnit4SpringContextTests {

        // Inject all repositories:
        @Autowired
        private IMyRepository myRepository;
    }

for example, in my applicationContext-mongo.xml i have that:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="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.2.xsd         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:META-INF/spring/database.properties"></property>
    </bean>

    <mongo:db-factory dbname="${mongo.database}" host="${mongo.host}" id="mongoDbFactory" port="${mongo.port}" write-concern="SAFE" />

    <mongo:repositories base-package="fullpackage.repositories" />       

    <!-- To translate any MongoExceptions thrown in @Repository annotated classes -->
    <context:annotation-config />

    <bean class="org.springframework.data.mongodb.core.MongoTemplate" id="mongoTemplate">
        <constructor-arg ref="mongoDbFactory" />
    </bean>


</beans>  

The:

    <mongo:repositories base-package="fullpackage.repositories" />       

allows spring to automatically instantiate your repository when the annotation @Autowired is found.

More simple, more properly. Personaly, i prefer this method.

Upvotes: 1

Related Questions