I am writing a test where I want to verify that a method is being invoked from within a void method. To clarify:
- In the class method
saveMappingsEntityList, it callspublishMappingUpdateEventand within that method, thepublishEventAsyncmethod is what I want to verify is being invoked once in the test - Currently in my test, the line
verify(pubSubClient, times(1)).publishMappingUpdateEvent(mappingsEntityList);is successfully being passed. The test fails though because it returns an error stating thatpublishEventAsyncwas not wanted but not invoked in regards to the secondverifyline.
Class method:
public void saveMappingsEntityList(List<MappingsEntity> mappingsEntityList) {
if (mappingsEntityList != null && !mappingsEntityList.isEmpty()) {
List<DynamoDBMapper.FailedBatch> failedBatches = dynamoDBMapper.batchSave(mappingsEntityList);
if (failedBatches != null && !failedBatches.isEmpty()) {
DynamoDBMapper.FailedBatch failedBatch = failedBatches.get(0);
log.error("Failed to save feature settings in dynamo db {}", failedBatch.getUnprocessedItems());
}
}
pubSubClient.publishMappingUpdateEvent(mappingsEntityList);
}
The void Method where I want to verify that the inside publishEventAsync method is being called
public void publishMappingUpdateEvent(List<MappingsEntity> mappingsEntityList) {
if (mappingsEntityList != null || !mappingsEntityList.isEmpty()) {
String companyId = mappingsEntityList.get(0).getCompanyId();
ErpPackage erpPackage = null;
try {
erpPackage = erpPackageRepository.getSelectedErpPackageForCompanyId(companyId);
} catch (NotFoundException e) {
logger.error("This entity is not registered with ECP - companyId: " + companyId);
}
try {
publishEventAsync(
new MappingUpdatedEvent(
erpPackage.getPartnerId(), erpPackage.getCompanyId(),
erpPackage.getErpId(), this.requestInfo.getCorrelationId(),
null
)
);
} catch (Exception e) {
logger.error("MappingsService::publishMappingUpdatedEvent: Failed to publish MappingUpdatedEvent for {}", erpPackage.getCompanyId(), e);
}
}
}
Test Method
@Test
public void testUpdateMappingsEntityList() throws Exception {
List<MappingsEntity> mappingsEntityList = DataUtil.getDdbResult()
.stream()
.collect(Collectors.toList());
ErpPackage erpPackage = new ErpPackage();
erpPackage.setId("test");
erpPackage.setPartnerId("partnerId");
erpPackage.setCompanyId("companyId");
erpPackage.setErpId("erpId");
when(erpPackageRepository.getSelectedErpPackageForCompanyId(anyString())).thenReturn(erpPackage);
when(this.requestInfo.getCorrelationId()).thenReturn("correlationId");
mappingsRepository.saveMappingsEntityList(mappingsEntityList);
doNothing().when(pubSubClient).publishMappingUpdateEvent(mappingsEntityList);
verify(pubSubClient, times(1)).publishMappingUpdateEvent(mappingsEntityList);
verify(pubSubClient, times(1)).publishEventAsync(any());
}