英文:
Fongo - Fake Mongo : Not able to load dataset from location for unit testing of mongrepository using fongo
问题
我正在使用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")
public void findUser_should_return_user() {
User user = userRepository.findByXYZId("XX12345");
assertNotNull(user);
}
缺少什么?需要怎样改变才能将数据集从JSON加载到fongo(假的Mongo)中?
[编辑-1] 需要尝试两件事情 #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
英文:
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
答案1
得分: 0
以下是您要翻译的内容:
对于测试,我建议使用这个库 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();
}
}
英文:
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();
}
}
答案2
得分: 0
以下是您提供的内容的翻译:
问题 -
- 不正确的 JSON 文件格式 - 附带的 JSON 供参考。我忘了在 JSON 中添加 "user" 集合。
- 文件的位置应该在资源目录中
/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);
}
}
英文:
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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论