Code Coverage for Salesforce/Apex

Hi, I’m analyzing the Apex code for salesforce project using Azure Devops. I get results in SonarCloud But, the coverage code is always 0.
This is the configuration used :

- task: SonarCloudPrepare@1


      SonarCloud: 'sonarcloud'

      organization: '**********'

      scannerMode: 'CLI'

      configMode: 'manual'

      cliProjectKey: 'project_key'

      cliProjectName: 'project_name'

      cliSources: '.'

      extraProperties: |






  - task: SonarCloudAnalyze@1

  - task: SonarCloudPublish@1


      pollingTimeoutSec: '300'

Any help please?

Hey there.

Check your analysis logs for Test coverage Sensor for Apex, anything interesting?

Hi, I removed the project in SonarCloud, I created another one and I analyzed again the salesforce project using Azure Devops. the result is different. The coverage code is 19.8%

When I checked the analyzed files, I found only 28 files analyzed.

When I checked in the logs , I found 150 files to be analyzed.
this is the logs

INFO: Sensor Apex Sensor [sonarapex]
INFO: 150 source files to be analyzed
INFO: 150/150 source files have been analyzed
INFO: Sensor Apex Sensor [sonarapex] (done) | time=5319ms
INFO: Sensor Test coverage Sensor for Apex [sonarapex]
##[error]ERROR: Invalid coverage information on file: 'OcicatPortalRecapController'
java.lang.IllegalStateException: Line 2629 is out of range in the file force-app/main/default/classes/OcicatPortalRecapController.cls (lines: 2625)
	at org.sonar.api.utils.Preconditions.checkState(
	at org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage.validateLine(
	at org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage.lineHits(
	at com.A.A.D.E.A(Unknown Source)
	at com.A.A.D.E.A(Unknown Source)
	at com.A.A.D.E.execute(Unknown Source)


WARN: File 'AssignAccountOwner' is present in coverage report but cannot be found in filesystem
WARN: File 'UserHierarchyUpdateSendEmail' is present in coverage report but cannot be found in filesystem
WARN: File 'UpdateAccountDetails' is present in coverage report but cannot be found in filesystem
WARN: File 'PopulateIncidentAccount' is present in coverage report but cannot be found in filesystem
WARN: File 'AddToModifyAllIncidentsGroup' is present in coverage report but cannot be found in filesystem
INFO: Sensor Test coverage Sensor for Apex [sonarapex] (done) | time=92ms
INFO: Sensor CSS Rules [javascript]
INFO: No CSS, PHP, HTML or VueJS files are found in the project. CSS analysis is skipped.
INFO: Sensor CSS Rules [javascript] (done) | time=2ms

Hey there.

This error message means that there is coverage being reported on line 2629 of a file, but as far as the scanner is aware, the file only has 2625 files.

Is the coverage report being freshly generated before the analysis, or is it possible an older coverage report is being used?

It looks like you’re performing a Pull Request Analysis, which means only changed files will be available on SonarCloud, although more files were analyzed at analysis time (and the results filtered later).

The coverage report is freshly generated, just before the analysis (using azure pipeline).
How can I modify the type “Pull Request Analysis” to get all analysis result ?

And taking a look at OcicatPortalRecapController.cls, how many lines does it actually have on the branch you are scanning?

You’ll need to make sure you’re building the main branch or a long-lived branch (which is determined by a regex, as noted in the docs) and building outside the context of a pull request in Azure DevOps.

the file OcicatPortalRecapController.cls has 2646 lines.

Ah thank you, the building branch is short-lived branch. I will change the type and analyze again.

Here is what I suggest: Once you have a full analysis running, if you still receive this specific error on coverage take a screenshot of where the file ends after browsing the Code tab, with the line numbers visible.

Now, all files have been analyzed. I got the same error for one file.

at org.sonar.scanner.bootstrap.GlobalContainer.doAfterStart(
at org.sonar.core.platform.ComponentContainer.startComponents(
at org.sonar.core.platform.ComponentContainer.execute(
at org.sonar.batch.bootstrapper.Batch.doExecute(
at org.sonar.batch.bootstrapper.Batch.execute(
at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(
at java.base/java.lang.reflect.Method.invoke(
at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(
at com.sun.proxy.$Proxy0.execute(Unknown Source)
at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(
at org.sonarsource.scanner.api.EmbeddedScanner.execute(
at org.sonarsource.scanner.cli.Main.execute(
at org.sonarsource.scanner.cli.Main.execute(
at org.sonarsource.scanner.cli.Main.main(

Also, when I executed Unit tests using sfdx cmd (sfdx force:apex:test:run …) the coverage code was 75%. But the value with sonarCloud is 11%
Have you any idea why values are not the same?

It looks like the logs you shared in your last post weren’t complete, can you make sure to include the full log message?

Are there non-Apex files (for example, Javascript) files being analyzed in your project?

The best way to debug issues like coverage numbers not matching is to narrow down to a specific example (a file, a line) where the coverage being reported by your coverage tool doesn’t match what is in your coverage report.

I excluded all non-Apex files, I analyzed again the project and I checked the percent by file. It is OK! Thank you for your help!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.