如何模拟mongoTemplate.aggregation()。getUniqueMappedResults()?

为什么以下内容在RepoClass中返回NullPointerException

mongoTemplate.aggregate(aggregation, COLLECTION_NAME,ScreenDetailsEntity.class);


我是Spring的新手,并且是第一次编写测试用例。我好累。
请帮忙。提前致谢。

RepoTestClass(测试功能):

  @Test
  public void testValidGetScreenDetails() {
    ReflectionTestUtils.setField(
        screenDetailsRepoService, "screenFields",
        ClassUtil.getFieldName(ScreenDetailsEntity.class));
    Aggregation aggregation1 = Aggregation.newAggregation(
        match(where(PAGE).is("Bookings")),
        project(ClassUtil.getFieldName(ScreenDetailsEntity.class)),
        limit(1)
    );

    MongoTemplate mongoTemplate = Mockito.mock(MongoTemplate.class);
    Aggregation aggregation = Mockito.mock(Aggregation.class);
    ScreenDetailsEntity screenDetailsEntity = Mockito.mock(ScreenDetailsEntity.class);
    AggregationResults<ScreenDetailsEntity> aggregationResultsMock = Mockito
        .mock(AggregationResults.class);
    Mockito.when(aggregationResultsMock.toString()).thenReturn(new String());
    Mockito.doReturn(aggregationResultsMock).when(mongoTemplate).aggregate(aggregation1, COLLECTION_NAME, ScreenDetailsEntity.class);

    Mockito.doReturn(screenDetailsEntity).when(aggregationResultsMock).getUniqueMappedResult();
    screenDetailsRepoService.getScreenDetails(
        "Bookings", "Web8", "List/View", "en", 45429, 121);
  }


RepoClass:

public class ScreenDetailsRepoService {

  @Autowired
  private MongoTemplate mongoTemplate;

  private String[] screenFields;

  @PostConstruct
  public void init() {
    screenFields = ClassUtil.getFieldName(DetailsEntity.class);
  }

  public ScreenDetailsEntity getScreenDetails(final @NonNull String page,
      final @NonNull String client, final String module,
      final String locale, final Integer hotelId, final Integer countryId
  ) throws NoDataFoundException {
    ScreenDetailsEntity screenDetailsEntity;
    Aggregation aggregation = newAggregation(
        match(where(PAGE).is(page)),
        project(screenFields),
        limit(1)
    );
    long t1 = System.currentTimeMillis();
    screenDetailsEntity = mongoTemplate.aggregate(aggregation, COLLECTION_NAME, ScreenDetailsEntity.class).getUniqueMappedResult();
    log.info(
        "DB Query: ScreenDetailsRepoService : getScreenDetails - Time taken for db requests : {} milliseconds",
        (System.currentTimeMillis() - t1));
    if (screenDetailsEntity == null) {
      log.error(
          "ScreenDetailsRepoService::getScreenDetails - No data found for for page {} ,module {}, locale {}, hotel {}, country {}",
          page, module, locale, hotelId, countryId);
      throw new NoDataFoundException("No data found");
    }
    return screenDetailsEntity;
  }

最佳答案

我最好的猜测是模拟设置方法中的Aggregation参数

Mockito.doReturn(aggregationResultsMock).when(mongoTemplate).aggregate(aggregation1, COLLECTION_NAME, ScreenDetailsEntity.class);


不等于mongoTemplate.aggregate中对getScreenDetails的调用中的实际值。 Mockito使用equals方法检查参数是否匹配,以及是否找不到匹配的模拟设置,将默认返回null

作为第一个测试,您可以尝试模拟任何Aggregation参数的方法。例如。

Mockito.doReturn(aggregationResultsMock).when(mongoTemplate).aggregate(Mockito.any(Aggregation.class), Mockito.eq(COLLECTION_NAME), Mockito.eq(ScreenDetailsEntity.class));


这样,您可以检查Aggregation参数是否引起了问题。

然后,应确保正确地实现了equals类的hashCodeAggregation方法。

09-26 17:10