3

I'm trying to use this library in some of my Spring application tests but without much success. It seems Spring Data MongoDB is supported but I did not manage to get it working. The examples do not follow completely the @Repository approach.

Let's say I have a DummyClass POJO:

@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;
    }

}

And I have the respective repository interface and custom interface/implementation:

@Repository
public interface DummyClassRepository extends MongoRepository<DummyClass, Long>, DummyClassRepositoryCustom {
}

public interface DummyClassRepositoryCustom {
    /* My dummy methods declaration */
}

public class DummyClassRepositoryImpl implements DummyClassRepositoryCustom{
    /* My dummy methods implementation */
}

This is how my test-context looks like (simplified):

<?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"
       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">

    <context:component-scan base-package="org.company.project.data.*"/>

    <!-- Fongo config (used by NoSQL-Unit) -->
    <bean name="fongo" class="com.foursquare.fongo.Fongo">
        <constructor-arg value="InMemoryMongo" />
    </bean>
    <bean id="mongo" factory-bean="fongo" factory-method="getMongo" />

    <mongo:db-factory id="mongoDbFactory" mongo-ref="mongo" />

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

    <mongo:repositories base-package="org.company.project.data.repositories.mongodb"/>

    <!-- Some other beans declaration -->

</beans>

Obviously my MongoDB repositories are under org.company.project.data.repositories.mongodb.

The issue is that I am not able to load a certain .json to the in-memory MongoDB instance. What I have so far:

@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);
    }

}

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:

{
    "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"
        }
    ]
}

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.

Did anyone work with this JUnit extension that could provide me some ideas why the specified dataset is not being loaded into the database?

Thank you in advance!

3
  • 1
    jaranda, after you comment about nosqlunit and this post, I've tested nosqlunit and works fine for me. What could be happening to me, is that MongoDbRule and DummyClassRepository are connecting to differents databases. MongoDbRule is using "test" database and isn't clear which database DummyClassRepository is using. In my MongoTemplate config, I explicitly set the database that should be used. As I told in another question, I'm not using xml to config Spring, at least on my tests. Commented Jun 27, 2013 at 11:48
  • 1
    In all examples on spring data documentation, they using dbname parameter on db-factory: <mongo:db-factory dbname="database" mongo-ref="mongo"/>. Try to specify "test" as dbname Commented Jun 27, 2013 at 11:54
  • @MiguelCartagena how I missed that! It works now, thanks! If you add your reply as answer I will accept it with pleasure :) Gracias! Commented Jun 27, 2013 at 14:58

2 Answers 2

3

if you want a good example on how to do this. check out johnathan mark smiths git example at https://github.com/JohnathanMarkSmith/spring-mongo-demo

Sign up to request clarification or add additional context in comments.

Comments

2

Jaranda,

Seems that your spring repository and nosqlunit are working on different databases. Try to modify your spring configuration to use the same database of nosqlunit.

 <mongo:db-factory id="mongoDbFactory" mongo-ref="mongo" dbname="test" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.