简体   繁体   English

你如何在 Mongo 中测试 listCollections

[英]How do you test listCollections in Mongo

I have a spring-boot project, with spring-data-mongo dependency.我有一个 spring-boot 项目,具有spring-data-mongo依赖性。 Everything is working great.一切都很好。 I have a service which basically does a listCollections on one of the DB and clears the content of all the collections in that DB.我有一项服务,它基本上在其中一个数据库上执行 listCollections 并清除该数据库中所有 collections 的内容。

Code:代码:

  public void clearContentsOfAllCollections() {
    MongoDatabase db = this.mongoTemplate.getMongoDbFactory().getDb("any-db-name");
    LOGGER.info("=================================================================");
    LOGGER.info("Clearing collections in DB - {}", db.getName());
    MongoIterable<String> collectionNames = db.listCollectionNames();
    for (final String collectionName : collectionNames) {
      LOGGER.info("Clearing collection - {}", collectionName);
      db.getCollection(collectionName).deleteMany(new Document());
    }
    LOGGER.info("Successfully cleared DB - {}", db.getName());
    LOGGER.info("=================================================================");
  }

When I try to write unit test, this is what I have当我尝试编写单元测试时,这就是我所拥有的

  @Test
  public void dropDBTest() {
    SimpleMongoClientDbFactory simpleMongoClientDbFactory = Mockito.mock(SimpleMongoClientDbFactory.class);
    MongoDatabase mongoDatabase = Mockito.mock(MongoDatabase.class);
    Mockito.when(mongoTemplate.getMongoDbFactory()).thenReturn(simpleMongoClientDbFactory);
    Mockito.when(simpleMongoClientDbFactory.getDb("db-name"))
      .thenReturn(mongoDatabase);

  // How do I convert this array list into a Mongo iterable 
     List<String> collectionList = Arrays.asList("collection-1", "collection-2");
  }

The problem is - I do not know how I can return the content of collectionList, when I have to mock database.listCollectionNames() (from package com.mongodb.client ).问题是 - 当我必须模拟database.listCollectionNames() (来自package com.mongodb.client )时,我不知道如何返回 collectionList 的内容。

Morever, the iterable.iterator() is of type MongoCursor .此外, iterable.iterator() 是MongoCursor类型。 How do I test this?我如何测试这个? Am I missing something?我错过了什么吗?

public class MongoDBTest {
            
         @Test
         public void testListCollectionNames() {
                // Set up the mock MongoDatabase
                MongoDatabase mockDatabase = Mockito.mock(MongoDatabase.class);
                // Set up the mock MongoIterable<String>
                MongoIterable<String> mockIterable = Mockito.mock(MongoIterable.class);
        
        
     Mockito.when(mockDatabase.listCollectionNames()).thenReturn(mockIterable);
             
        final var collectionNames = new ArrayList<>();
        collectionNames.add("testCollection")
        doReturn(collectionNames).when(mockIterable).into(new ArrayList<>());
                
            
                // Get the list of collection names
                List<String> collectionNames = mockDatabase.listCollectionNames().into(new ArrayList<>());
            
                // Assert that the test collection is in the list
                assertEquals(true, collectionNames.contains("testCollection"));
              }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM