您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

使用,MongoTemplate#indexOps(String collection)您可以获取的列表IndexInfo,代表MongoDB集合的索引。由于这是一个常规列表,因此您可以结合使用hasItem(Matcher<? super T> itemMatcher)和进行断言hasProperty(String propertyName, Matcher<?> valueMatcher)

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"))))));

如果您觉得这太难以理解或不方便使用,最好使用自定义的Hamcrest匹配器。

mongodb 2022/1/1 18:30:21 有490人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶