How to verify methods that are called within a void method in Mockito?

Viewed 31

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 calls publishMappingUpdateEvent and within that method, the publishEventAsync method 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 that publishEventAsync was not wanted but not invoked in regards to the second verify line.

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());
    }
0 Answers
Related