gpt4 book ai didi

java - Java junits 中的模拟 CosmosClient

转载 作者:行者123 更新时间:2023-12-03 06:51:56 38 4
gpt4 key购买 nike

我们有一个要求,即从 Azure-CosmosDB - sql api 获取少量记录为了增加覆盖范围,我们想要模拟,但已经搜索了很多,但没有找到 Java。请您在这里指导一下

我的 DAO 类如下

private CosmosClient client;

@Autowired
public DAO(CosmosClient client) {
this.client = client;
}

CosmosDatabase database;
CosmosContainer container;

private static final String DATABASE = "test-db";
private static final String CONTAINER = "test-container";

/**
* Test method
*/
public List<TestVO> fetchTestData() {

// create database
CosmosDatabaseResponse databaseResponse = client.createDatabaseIfNotExists(DATABASE);
database = client.getDatabase(databaseResponse.getProperties().getId());

// create container
CosmosContainerProperties containerProperties = new CosmosContainerProperties(CONTAINER, "/pkey");
CosmosContainerResponse containerResponse = database.createContainerIfNotExists(containerProperties,
throughputProperties);
container = database.getContainer(containerResponse.getProperties().getId());

// Form querySpec
Iterable<FeedResponse<Test>> feedRespIter = container
.queryItems(querySpec, queryOptions, Test.class).iterableByPage(continuationToken, pageSize);

// iterate list of documents to return shipping list
if (feedRespIter .iterator().hasNext()) {
// Some business logic
}
}

在向 cosmosClient 注入(inject)模拟或只是模拟时,我收到错误

Cannot mock/spy class com.azure.cosmos.CosmosClient
Mockito cannot mock/spy because
- final class
at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.initMocks(MockitoTestExecutionListener.java:83)
at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.prepareTestInstance(MockitoTestExecutionListener.java:60)

最佳答案

CosmosClient 是一个最终类,要模拟最终类,您需要使用以下步骤配置 Mockito 功能:

在以下路径中创建文件:

src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker

包含以下内容:

mock-maker-inline

这应该启用最终类的模拟。

关于java - Java junits 中的模拟 CosmosClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73529141/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com