当我想为 2 个查询返回不同的结果时如何模拟 ReaciveMongoTemplate 聚合方法

问题描述 投票:0回答:0

`我正在为我的 DAO 层编写 Junit 测试用例。我的 Update 方法获取文档的详细信息,如果存在则更新该文档。 当我尝试模拟 ReactiveMongoTemplate 聚合方法时,它返回 null

这是我的 DAO 方法的片段:

 @Autowired
private ReactiveMongoTemplate mongoTemplate;

public void method() {

//some code

MatchOperation match = Aggregation.match(new Criteria(FolderConfigurationServiceConstants.DOCUMENT_TYPE)
                .is(DocumentTypes.ROOT));
Aggregation aggregation = Aggregation.newAggregation(match);
        
Flux<MongoDocumentDetails> document = mongoTemplate.aggregate(aggregation, collection, MongoDocumentDetails.class);

//some code
}`
```

and another aggregation operation is :

`MatchOperation match = Aggregation.match(new Criteria(FolderConfigurationServiceConstants.DOCUMENT_TYPE)
                .is(DocumentTypes.DIRECTORY)
                .andOperator(Criteria.where("name").is(name),                       Criteria.where(FolderConfigurationServiceConstants.PARENT_PATH).is(fetchGrandParent(parentPathArray, lengthOfArray).toString())));
Aggregation aggregation = Aggregation.newAggregation(match);
        
Flux<MongoDocumentDetails> parentDocument = mongoTemplate.aggregate(aggregation, collection, MongoDocumentDetails.class);`

Now, I want to mock the aggregate method with a particular response when mongotemplate calls the aggregate method. How can I do this? I want to have 2 different results.

**My Test method looks like :**


`@Test
void method()
{
//some code
  Mockito.doReturn(Flux.just(getDocument()))
        .when(mongoTemplate).aggregate(aggregationForRoot, COLLECTION, MongoDocumentDetails.class);
//ideally it should return a document on execution but returns null
}`
`
mongodb spring-boot junit aggregation-framework reactivemongo
© www.soinside.com 2019 - 2024. All rights reserved.