Junit test failed with error "Invalid comment format line"

Try to update existing customized java plugin:

From versions (sonarqube-10.5.1.90531, all tests passed under these versions)
sonar-plugin-api: 10.7.0.2191
sonar-java-plugin: 7.33.0.35775

To versions (sonarqube-10.6.0.92116, many tests failed):
sonar-plugin-api: 10.9.0.2362
sonar-java-plugin: 8.0.1.36337

Many junt tests failed under sonar-plugin-api: 10.9.0.2362 & sonar-java-plugin: 8.0.1.36337 with error messages: Invalid comment format line <number> ...

Test data example being complained:

// Noncompliant [[sc=26;ec=30]] {{Rename this field ....}}

If we move the [[sc=26;ec=30]] to the end of the comment (please see below), junit test will pass:

// Noncompliant {{Rename this field ....}} [[sc=26;ec=30]] 

Error stack (sonar-analyzer-common 2.11.0.2861):

java.lang.IllegalStateException: Invalid comment format line x:  Noncompliant [[sc=26;ec=30]] {{Rename this ....}}
	at org.sonarsource.analyzer.commons.checks.verifier.internal.NoncompliantCommentParser.parse(NoncompliantCommentParser.java:55)
	at org.sonarsource.analyzer.commons.checks.verifier.internal.FileIssues.<init>(FileIssues.java:54)

I checked sonar-java-8.0.1.36337, the format of // Noncompliant [[sc=x;ec=x]] {{....}} are still in their codes.
Could you please help? thanks

Thanks @ct24p , thanks for contributing to the community.

The upgrade you mentioned involved a major version change that, in this case, had a breaking change feature regarding tests and the format for the Noncompliant comment. This change aligned internal and external comment parsers using the same regex.

Unfortunately this has impacted negatively in your code, apologies for that. You will have to modify your code and adapt it to the new format.

In order to make an easier and safer transition I would use the opportunity to mention that there’s a better and easier way to point to the part of the code that should trigger the issue instead of using parameters.

Using a comment full of ^ chars that point to the above line with the code that will raise the issue, as you can see in these examples.

      S3Client s3Client = S3Client.builder().region(Region.EU_CENTRAL_1).build(); // Noncompliant {{Instantiate this client outside the Lambda function.}}
//                                                                       ^^^^^
      S3Client.builder().build(); // Noncompliant {{Instantiate this client outside the Lambda function.}}
//                       ^^^^^
      MachineLearningClient.builder().build(); // Noncompliant {{Instantiate this client outside the Lambda function.}}
//                                    ^^^^^
      DriverManager.getConnection("foo"); // Noncompliant {{Instantiate this database connection outside the Lambda function.}}
//                  ^^^^^^^^^^^^^

Hope this helps.

2 Likes