0

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

StackOverFlow
  • 4,486
  • 12
  • 52
  • 87

2 Answers2

0

For testing I recommend this library this

<dependency>
    <groupId>de.flapdoodle.embed</groupId>
    <artifactId>de.flapdoodle.embed.mongo</artifactId>
    <scope>test</scope>
</dependency>

I think it's is very good library and tested in production grade. Embedded MongoDB will provide a platform neutral way for running mongodb in unittests.

In tests You can create simple method like @BeforeAll and populate data. I give You my example

@DataMongoTest
@ExtendWith(SpringExtension.class)
@DirtiesContext
class ItemReactiveRepositoryTest {


@Autowired
ItemReactiveRepository itemReactiveRepository;
List<Item> itemList = Arrays.asList(
        new Item(null, "Samsung TV", 400.0),
        new Item(null, "LG TV", 420.0),
        new Item(null, "Apple Watch", 420.0),
        new Item(null, "Beats Headphones", 149.99),
        new Item("ABC", "Bose Headphones", 149.99)
);

@BeforeEach
void setUp() {
    itemReactiveRepository.deleteAll()
            .thenMany(Flux.fromIterable(itemList))
            .flatMap(item -> itemReactiveRepository.save(item))
            .doOnNext(item -> System.out.println("Inserted item is :" + item))
            .blockLast();
}

@Test
public void getAllItems() {
    Flux<Item> all = itemReactiveRepository.findAll();
  StepVerifier.create(all).expectSubscription().expectNextCount(5).verifyComplete();
        }
     }
merc-angel
  • 394
  • 1
  • 5
  • 13
  • Thanks for response. I tried flapdoodle but when its trying to download req zip outside corporate network : unable-to-download-embedded-mongodb-behind-proxy-using-automatic-configuration. I ref this solution https://stackoverflow.com/a/45597381/297907 – StackOverFlow Aug 06 '20 at 19:08
0

Issues -

  • Incorrect json file format -- Attached json for reference. I missed to add "user" collection in json
  • locations of file should be in resources /user.json or /../../user.json

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);
            }
    
}
StackOverFlow
  • 4,486
  • 12
  • 52
  • 87