title: NoSQLUnit MongoDB test: Spring Data MongoDB approach
body: <p>I'm trying to use <a href="https://github.com/lordofthejars/nosql-unit/" rel="nofollow">this</a> library in some of my Spring application tests but without much success. It seems Spring Data MongoDB is <a href="https://github.com/lordofthejars/nosql-unit#mongodb-engine" rel="nofollow">supported</a> but I did not manage to get it working. The <a href="http://www.lordofthejars.com/2013/01/testing-spring-data-mongodb.html" rel="nofollow">examples</a> do not follow completely the @Repository approach.</p>
  
  <p>Let's say I have a DummyClass POJO:</p>
  
  <pre><code>@Document(collection = DummyClass.ITEMS_COLLECTION_NAME)
  public class DummyClass {
  
      public static final String ITEMS_COLLECTION_NAME = "dummy";
  
      //Maps _id mongodb internal field
      private BigInteger id;
  
      private String a;
      private String b;
  
      public DummyClass() {
          super();
      }
  
      public DummyClass(String a, String b) {
          this.a = a;
          this.b = b;
      }
  
      public String getA() {
          return a;
      }
  
      public void setA(String a) {
          this.a = a;
      }
  
      public String getB() {
          return b;
      }
  
      public void setB(String b) {
          this.b = b;
      }
  
  }
  </code></pre>
  
  <p>And I have the respective repository interface and custom interface/implementation:</p>
  
  <pre><code>@Repository
  public interface DummyClassRepository extends MongoRepository&lt;DummyClass, Long&gt;, DummyClassRepositoryCustom {
  }
  
  public interface DummyClassRepositoryCustom {
      /* My dummy methods declaration */
  }
  
  public class DummyClassRepositoryImpl implements DummyClassRepositoryCustom{
      /* My dummy methods implementation */
  }
  </code></pre>
  
  <p>This is how my test-context looks like (simplified):</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"
         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"&gt;
  
      &lt;context:component-scan base-package="org.company.project.data.*"/&gt;
  
      &lt;!-- Fongo config (used by NoSQL-Unit) --&gt;
      &lt;bean name="fongo" class="com.foursquare.fongo.Fongo"&gt;
          &lt;constructor-arg value="InMemoryMongo" /&gt;
      &lt;/bean&gt;
      &lt;bean id="mongo" factory-bean="fongo" factory-method="getMongo" /&gt;
  
      &lt;mongo:db-factory id="mongoDbFactory" mongo-ref="mongo" /&gt;
  
      &lt;bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"&gt;
          &lt;constructor-arg ref="mongoDbFactory"/&gt;
      &lt;/bean&gt;
  
      &lt;mongo:repositories base-package="org.company.project.data.repositories.mongodb"/&gt;
  
      &lt;!-- Some other beans declaration --&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Obviously my MongoDB repositories are under <code>org.company.project.data.repositories.mongodb</code>.</p>
  
  <p>The issue is that I am not able to load a certain .json to the in-memory MongoDB instance. What I have so far:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations={"/test-context.xml"})
  public class MyTest {
  
      private static Logger logger = Logger.getLogger(MyTest.class);
  
      @Autowired
      private ApplicationContext applicationContext;
  
      @Rule
      public MongoDbRule mongoDbRule = newMongoDbRule().defaultSpringMongoDb("test");
  
      @Autowired
      private DummyClassRepository dummyClassRepository;
  
      @Test
      @UsingDataSet(locations = {"/dummy.json"}, loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
      public void myTest(){
           logger.info(dummyClassRepository.findAll().size());
           assert(false);
      }
  
  }
  </code></pre>
  
  <p>The content of the .json file follows the format specified in the docs, but regardless of what I specify, the collection is always empty when starting a test method. For example, an example of this .json file could be:</p>
  
  <pre><code>{
      "dummy": [
          {
              "_class": "org.company.project.data.model.DummyClass",
              "_id": "51557362e4b083f9e619f99c",
              "a": "a",
              "b": "b"
          },
          {
              "_class": "org.company.project.data.model.DummyClass",
              "_id": "51557362e4b083f9e619f99d",
              "a": "c",
              "b": "d"
          }
      ]
  }
  </code></pre>
  
  <p>What shocks me is that once, within the test method itself, I was able to add my dummy instances to the database without problems, which basically tells me the repository is actually working fine.</p>
  
  <p>Did anyone work with this JUnit extension that could provide me some ideas why the specified dataset is not being loaded into the database?</p>
  
  <p>Thank you in advance!</p>