【问题标题】:Fongo - Fake Mongo : Not able to load dataset from location for unit testing of mongrepository using fongoFongo - Fake Mongo:无法从位置加载数据集以使用 fongo 对 mongrepository 进行单元测试
【发布时间】:2020-11-27 02:12:28
【问题描述】:

我在内存数据库中使用 fongo 来测试我的 mongodbrepository。

我参考了http://dontpanic.42.nl/2015/02/in-memory-mongodb-for-unit-and.html 进行单元测试。

为了填充示例数据,我在 test/resources/json-data/user/user.json 下添加了所需的 json 文件,但它没有加载到 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);
    }

缺少什么?将数据集从 json 加载到 fongo(假 mongo)需要改变什么

[Edit-1] 需要尝试 2 件事 #1 包括缺少的规则和 #2 json 格式

看起来 json 格式需要包含集合名称 - 参考1:https://github.com/lordofthejars/nosql-unit#dataset-format

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

【问题讨论】:

    标签: java mongodb junit fongo


    【解决方案1】:

    为了测试,我推荐这个库 this

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

    我认为这是一个非常好的库,并在生产级进行了测试。嵌入式 MongoDB 将为在单元测试中运行 mongodb 提供一种平台中立的方式。

    在测试中你可以创建像@BeforeAll 这样的简单方法并填充数据。我给你我的榜样

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

    【讨论】:

    • 感谢您的回复。我尝试了fladdoodle,但是当它尝试在公司网络之外下载req zip时:无法下载-嵌入式-mongodb-behind-proxy-using-automatic-configuration。我参考这个解决方案stackoverflow.com/a/45597381/297907
    【解决方案2】:

    问题 -

    • 不正确的 json 文件格式 -- 附加 json 供参考。我错过了在 json 中添加“用户”集合
    • 文件的位置应该在资源中 /user.json 或 /../../user.json

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

    我的工作答案。

    user.json

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

    测试用例

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      • 2018-04-29
      • 2019-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多