jarandaf
jarandaf

Reputation: 4427

NoSQLUnit MongoDB test: Spring Data MongoDB approach

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!

Upvotes: 3

Views: 3952

Answers (2)

user2428795
user2428795

Reputation: 547

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

Upvotes: 3

Miguel Cartagena
Miguel Cartagena

Reputation: 2606

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

Upvotes: 2

Related Questions