Java 如何在集成测试中测试Mongo索引?

Java 如何在集成测试中测试Mongo索引?,java,mongodb,indexing,integration-testing,Java,Mongodb,Indexing,Integration Testing,我有一个Java方法,它在Mongo集合的两个字段上创建索引。 我应该获取集合的索引信息,然后检查索引的名称和字段是否正确。 为此编写集成测试的最干净的方法是什么?使用自定义Hamcrest匹配器查看索引是否在集合中有意义吗?在春季 使用MongoTemplate#indexOps(字符串集合)可以获取IndexInfo的列表,表示MongoDB集合的索引。因为这是一个常规列表,所以您可以结合使用hasItem(Matcher-valueMatcher): final List index=mo

我有一个Java方法,它在Mongo集合的两个字段上创建索引。 我应该获取集合的索引信息,然后检查索引的名称和字段是否正确。 为此编写集成测试的最干净的方法是什么?使用自定义Hamcrest匹配器查看索引是否在集合中有意义吗?

在春季 使用
MongoTemplate#indexOps(字符串集合)
可以获取
IndexInfo
的列表,表示MongoDB集合的索引。因为这是一个常规列表,所以您可以结合使用
hasItem(Matcher-valueMatcher)

final List index=mongoTemplate.indexOps(“myCollection”).getIndexInfo();
资产(索引,hasSize(3));
资产(索引,hasItem(hasProperty(“名称”)为(“\u id”)));
资产(索引,hasItem(hasProperty(“名称”),为(“index1”));
资产(索引,hasItem(hasProperty(“名称”),为(“index2”));
资产(索引,hasItem(hasProperty(“indexFields”),hasItem(hasProperty(“key”),is(“field1”)));
如果你觉得这太不可读或不容易理解,你最好使用一个定制的Hamcrest匹配器

final List<IndexInfo> indexes = mongoTemplate.indexOps("myCollection").getIndexInfo();
assertThat(indexes, hasSize(3));
assertThat(indexes, hasItem(hasProperty("name", is("_id_"))));
assertThat(indexes, hasItem(hasProperty("name", is("index1"))));
assertThat(indexes, hasItem(hasProperty("name", is("index2"))));
assertThat(indexes, hasItem(hasProperty("indexFields", hasItem(hasProperty("key", is("field1"))))));