Code coverage issue of Apex class in Sonarqube

Hi Team,

We are getting significant difference in code coverage of same class in SonarQube and Salesforce sandbox.

We found discrepancy in code coverage and not able to generate sonar report properly regarding code quality.

For example below are the sample classes which we ran in sandbox and we got 100% code coverage, however, in SonarQube it is showing 0%.

Sandbox code coverage:
AsyncObeB2B_DeviceReservation_Test : 100%
B2BCmexAppHandlerV2Test: 100%
ConvertListtoStrTest: 100%

Sonarqube code coverage:
AsyncObeB2B_DeviceReservation_Test : 0%
B2BCmexAppHandlerV2Test: 0%
ConvertListtoStrTest: 0%

Is anyone face the same issue, then please share the solution. It will be helpful and highly appreciated.

As requested in the template post, what version of SonarQube are you using?

What do the logs say about the import of code coverage?

Hi Colin,
Thanks for your response.
Here are the details regarding SonarQube :
SonarQube Enterprise Edition
Version : 10.1.0.73491

There are two stages in our pipeline on Gitlab, one is Validation and the other one is Sonar.
In validation stage we are using below command to generate test-result-codecoverage.json file which is being used to show code coverage report in SonarQube.
Command:
Dsonar.apex.coverage.reportPath=/builds/crm/obe-sfvl-git-repo/test-result-codecoverage.json

Attaching one file with log content of a failed sonar stage.
logFileFailedSonarStage.txt (23.3 KB)

Thanks.

Do you see these classes respresnted in the raw /builds/crm/obe-sfvl-git-repo/test-result-codecoverage.json? SonarQube relies on the report having accurate information about the code coverage (it only reads the report, it’s not involved in deciding what code is covered or not).

And, I noticed you have lots of errors like this one:

ERROR: Invalid coverage information on file: 'OBE_I09_ManageScoring'
java.lang.IllegalStateException: Line 351 is out of range in the file force-app/main/default/classes/OBE_I09_ManageScoring.cls (lines: 288)

This would normally indicate that your coverage file is outdated (referencing lines that no longer exist in the file) – are you generating a fresh coverage file before each analysis?

Hi Colin,

The pipeline always generates a new test-result-codecoverage.json.

Okay. So I come back to this question:

Hi Colin,

We have some observation here when json file is getting generated some of the classes which we mentioned in our sonar scan command do not exist in json file. Thus it is not reflecting in SonarQube report.
But ideally it should be there since scan or validation stage gets passed only when coverage of all classes is more than 75%.

For your reference I am attaching the json file and screenshots of sonar report.
sonarqubeImages.zip (269.2 KB)
test-result-codecoverage.json (485.8 KB)

For test run below is the sample command we are using:

sfdx force:apex:test:run -n “AmortizationTableControllerTest,AsyncObeB2B_DeviceReservation_Test,AttributeUtility_Test,B2BCmexAppHandlerV2Test,ChangePasswordControllerTest,CommunitiesLandingControllerTest,CommunitiesLoginControllerTest,CommunitiesSelfRegConfirmControllerTest,CommunitiesSelfRegControllerTest,ConvertListtoStrTest,DataForAppointmentBook_Test,FixEnvelopeStatusScheduler_Test,FixPicklistValuesUpdateJSON_Test,ForgotPasswordControllerTest,INT_ClickThrough_Controller_Test,INT_IntegrationSettingsUtility_Test,INT_JSONConversionMappingUtility_Test,INT_SwagClient_Test,LightningForgotPasswordControllerTest,LightningLoginFormControllerTest,LightningSelfRegisterControllerTest,LOG_DeleteLogMessagesSchedulerTest,LOG_LogMessageUtilityTest,MockHttpResponseGeneratorTest,MyProfilePageControllerTest,OBEB2B_sendKCISMSTest,OBE_AccountDomainTest,OBE_AccountRepository_Test,OBE_AddDeviceTest,OBE_AddressValidationUpdate_Test,OBE_AllocateESIMAndGeneratePDFCntrlTest,OBE_Appointment_CancellationBatchTest,OBE_Asset_TriggerHandler_Test,OBE_AssetizationUtility_Test,OBE_AssetRepositoryTest,OBE_AssetStructureDecoderTest,OBE_B2B_AccountUtils_Test,OBE_BatchOrderGenerator3Test,OBE_BulkPromoActivationBatch_Test,OBE_BulkPromoActivationBatch2_Test,OBE_Cable_Address_SwagAreaApiTest,OBE_Cable_Address_SwagAreaTest,OBE_Cable_Address_SwagErrorTest,OBE_Cable_Address_SwagFuzzyTest,OBE_Cable_Address_SwagLocaCharacTest,OBE_Cable_Address_SwagOperatorTest,OBE_Cable_Address_SwagStreetApiTest,OBE_Cable_Address_SwagStreetTest,OBE_Cable_addressValidationTest,OBE_CancellationForInactivity_Test” --target-org TestACC -c -d /builds/crm/obe-sfvl-git-repo -r human

Hey there.

If the issue is that those files aren’t appearing in your JSON file… that all happens before it reaches SonarQube, and you probably want to reach out to Salesforce support!