Fongo - Fake Mongo:无法从位置加载数据集以使用 fongo 对 mongrepository 进行单元测试
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") // test/resources/..
public void findUser_should_return_user() {
User user = userRepository.findByXYZId("XX12345");
assertNotNull(user);
}
缺少什么?需要更改什么才能将数据集从 json 加载到 fongo(fake 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
为了测试我推荐这个库
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();
}
}
问题 -
- 不正确的 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);
}
}
我在内存数据库中使用 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(fake 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
为了测试我推荐这个库 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();
}
}
问题 -
- 不正确的 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);
}
}