Reputation: 101
We have a requirement where we are fetching few records from Azure-CosmosDB - sql api For increasing coverage, we want to mock but have searched a lot but didn't find for Java. Could you please guide here
My DAO class is as below
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
}
}
On injecting mock to cosmosClient or just mocking, I get error
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)
Upvotes: 0
Views: 1324
Reputation: 28
CosmosClient is a final class, and to mock final classes, you need to configure a Mockito feature using the below steps:
Create a file in the following path:
src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker
with the following content:
mock-maker-inline
This should enable Mocking of final classes.
Upvotes: 1