首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Fongo -假Mongo :无法从位置加载数据集,以便使用fongo对mongrepository进行单元测试。

Fongo -假Mongo :无法从位置加载数据集,以便使用fongo对mongrepository进行单元测试。
EN

Stack Overflow用户
提问于 2020-08-06 18:47:20
回答 2查看 226关注 0票数 0

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

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

为了填充示例数据,我在test/resources/ json - data /user/user.json下添加了必需的json文件,但它没有加载到fongo中。

代码语言:javascript
复制
@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(假芒果)的需要更改为什么?

编辑-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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-08-07 07:17:46

问题-

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

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

我的工作答案。

user.json

代码语言:javascript
复制
{
  "user": [
    {
      "_id": "XX12345",
      "ack": true,
      "ackOn": []
    }
  ]
}

测试用例

代码语言:javascript
复制
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);
            }
    
}
票数 0
EN

Stack Overflow用户

发布于 2020-08-06 19:03:40

为了进行测试,我推荐这个库

代码语言:javascript
复制
<dependency>
    <groupId>de.flapdoodle.embed</groupId>
    <artifactId>de.flapdoodle.embed.mongo</artifactId>
    <scope>test</scope>
</dependency>

我认为这是一个很好的图书馆,并在生产等级测试。嵌入式MongoDB将为在单元测试中运行mongodb提供一种平台中立的方法。

在测试中,您可以创建@BeforeAll这样的简单方法并填充数据。我给你举个例子

代码语言:javascript
复制
@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();
        }
     }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63289949

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档