模拟/替换从s3库返回的selectobjectcontenteventstream.getrecordsinputstream()

fbcarpbf  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(191)

我正试图围绕对awss3select的调用创建一些单元测试,但在模拟从getrecordsinputstream()返回的selectrecordsinputstream时遇到了问题。我无法示例化我自己的selectrecordsinputstream,因为该类是包私有的,并且我无法从mockito的返回inputstream的示例。thenreturn()需要selectrecordsinputstream的示例(这使我感到困惑,因为selectrecordsinputstream扩展了inputstream,您可以将此类型指定为在资源试用中返回)
我能做些什么(不引入另一个测试框架)来返回我自己的inputstream示例或selectrecordsinputstream吗?它也不能是模拟示例,因为测试围绕着处理inputstream展开。
生产代码:

List<StockAvailability> stockList = Collections.emptyList();

SelectObjectContentResult result = s3Client.selectObjectContent(query);

try (InputStream resultInputStream = result.getPayload().getRecordsInputStream()) {
   stockList = transformResponse(resultInputStream);
}
catch (IOException e) {
   LOG.error("Unable to parse stock availability response: {}", e.getMessage());
}

单元测试:

@Test
     void test1()  {
     final Query query = new Query.QueryBuilder().query(SELECT_QUERY)
                                               .bucket(BUCKET)
                                               .objectKey(KEY)
                                               .build();

     when(s3Client.selectObjectContent (query)).thenReturn(selectObjectContentResult);

     byte[] string = "Any String you want".getBytes();

     InputStream inputStream = new ByteArrayInputStream(string);
when(selectObjectContentResult.getPayload()).thenReturn(selectObjectContentEventStream);

     //Does not compile and cannot cast to SelectRecordsInputStream       
     when(selectObjectContentEventStream.getRecordsInputStream()).thenReturn(inputStream);

     final List<StockAvailability> result = stockService.queryStockData(query);
  }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题