Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 274
beku8 Avatar asked May 10 '26 09:05

beku8


1 Answers

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.

like image 183
Mentor Reka Avatar answered May 12 '26 05:05

Mentor Reka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!