javamongodbjunitfongo

Fongo - Fake Mongo : Not able to load dataset from location for unit testing of mongrepository using fongo


I am using fongo as in memory database for testing my mongodbrepository.

I have taken reference from http://dontpanic.42.nl/2015/02/in-memory-mongodb-for-unit-and.html for unit testing.

To populate sample data, I have added required json file under test/resources/json-data/user/user.json but it's not loaded into fongo.

@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations = "/json-data/user/user.json") // test/resources/..
    public void findUser_should_return_user() {
        User user = userRepository.findByXYZId("XX12345");
        assertNotNull(user);
    }

What's missing ? What needs to change to load dataset from json to fongo(fake mongo)

[Edit-1] Need to try 2 things #1 Include missing rule & #2 json format

looks like json format need to include collection name - Reference-1 : https://github.com/lordofthejars/nosql-unit#dataset-format

Reference 2 -https://github.com/lordofthejars/nosql-unit/tree/master/nosqlunit-demo/src/test/resources/com/lordofthejars/nosqlunit/demo/mongodb


Solution

  • Issues -

    Ref - https://github.com/lordofthejars/nosql-unit/issues/158

    My Working answer.

    user.json

    {
      "user": [
        {
          "_id": "XX12345",
          "ack": true,
          "ackOn": []
        }
      ]
    }
    

    Test case

    import com.myapp.config.FakeMongo;
    import com.myapp.domain.User;
    import com.lordofthejars.nosqlunit.annotation.UsingDataSet;
    import com.lordofthejars.nosqlunit.core.LoadStrategyEnum;
    import com.lordofthejars.nosqlunit.mongodb.MongoDbRule;
    import org.junit.Rule;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.Import;
    import org.springframework.test.context.ActiveProfiles;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import static com.lordofthejars.nosqlunit.mongodb.MongoDbRule.MongoDbRuleBuilder.newMongoDbRule;
    import static org.junit.Assert.assertNotNull;
    
    
    @ActiveProfiles({ "test", "unit" })
    @RunWith(SpringRunner.class)
    @Import(value = {FakeMongo.class})                                          
    public class UserRepositoryTest {
            @Autowired
            private UserRepository userRepository;
            
            @Autowired
            private ApplicationContext applicationContext;
            
            @Rule
            public MongoDbRule embeddedMongoDbRule = newMongoDbRule().defaultSpringMongoDb("mockDB");
        
            @Test
            @UsingDataSet(locations = "/user.json", loadStrategy = LoadStrategyEnum.CLEAN_INSERT) // test/resources/..
                public void findUser_should_return_user() {
                    User user = userRepository.findByXYZId("XX12345");
                    assertNotNull(user);
                }
        
    }