我正在使用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(假芒果)的需要更改为什么?
编辑-1需要尝试2件事情#1包括缺少规则& #2 json格式
看起来json格式需要包含集合名称-引用-1:https://github.com/lordofthejars/nosql-unit#dataset-format。
发布于 2020-08-07 07:17:46
问题-
参考文献- 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);
}
}发布于 2020-08-06 19:03:40
为了进行测试,我推荐这个库这
<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();
}
}https://stackoverflow.com/questions/63289949
复制相似问题