Todd Patch
Todd Patch

Reputation: 358

Adding a Sonar rule for the "assert" keyword in Java code

Over time, I have found that the usage of the "assert" keyword in java has caused more problems than what the developer was hoping to fix. Because of their "off by default" nature in production code, but "on" in test code that is run in Junit or Testng code, tracking down issues in their usage can be made more difficult.

Anyway, we started using SonarQube recently. I was hoping to find a rule that would point out the usage of the "assert" keyword, but I have not found one.

I was wondering if anyone else had a similar desire in this rule and possibly created a plugin for it?

Thanks!

Upvotes: 1

Views: 759

Answers (1)

Kraal
Kraal

Reputation: 2877

Try to create a squid:XPath custom rule with the following xpathQuery :

//assertStatement

It should detect all assert statements.

You can achieve the same result with PMD by creating a pmd:XPathRule custom rule. However the xpath is slightly different :

//AssertStatement

Upvotes: 3

Related Questions