Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring Data MongoDB: Unit tests with repositories
    primarykey
    data
    text
    <p>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:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;!-- Default bean name is 'mongo' --&gt; &lt;mongo:mongo host="${mongo.host}" port="${mongo.port}"&gt; &lt;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"/&gt; &lt;/mongo:mongo&gt; &lt;bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"&gt; &lt;constructor-arg ref="mongo" /&gt; &lt;constructor-arg name="databaseName" value="${mongo.db}" /&gt; &lt;/bean&gt; &lt;context:component-scan base-package="domain.company.group.project.data.repositories"/&gt; &lt;!-- MongoDB repositories --&gt; &lt;mongo:repositories base-package="domain.company.group.project.data.repositories.mongodb"/&gt; &lt;!-- some other stuff --&gt; &lt;/beans&gt; </code></pre> <p>And let's say I have a simple repository as follows:</p> <pre><code>public interface LocationRepository extends MongoRepository&lt;Location, String&gt;, LocationRepositoryCustom { } </code></pre> <p>where LocationRepositoryImpl is the class implementing all my custom methods for a certain Location (domain object) class. My test class looks like:</p> <pre><code>@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"/test-context.xml"}) public class LocationRepositoryTest { @Autowired private LocationRepository locationRepository; /* Some tests... */ } </code></pre> <p>I have tried to embed a MongoDB instance within my running tests (as explained <a href="http://blog.yohanliyanage.com/2012/11/integration-testing-mongodb-spring-data/" rel="nofollow">here</a>) 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.</p> <p>I am using Spring 3.2.0 and Spring Data Mongo 1.1.0.RELEASE. I am using Junit for testing.</p> <p>Any suggestions?</p> <p>Thank you in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload