Sonar Java: check the quality of the test classes source code?

pan40 picture pan40 · Jan 17, 2013 · Viewed 9.2k times · Source

Is it possible to check in Sonar the quality of the *Test.java source code, e.g. Methods maximum size 100 lines?

The problem is, that the Java Junit tests are growing with the productive code, also the complexity.

We have unit test classes with more than 1000 lines and 2 methods.

We want to check in Sonar some rules for these *Test.java classes.

Answer

Romain Linsolas picture Romain Linsolas · Jan 17, 2013

Since Sonar 3.1, it includes a plugin that has specific PMD rules to be executed against the unit tests (a JIRA was created for that). You can see them in the Configuration > Quality Profiles > Coding Rules.

However, it seems that you want to run a full analysis on the test source code, like you do on the production source code, and get additional metrics (for ex. a % rules compliance and also a % rules compliance for unit tests). I don't think that Sonar provides such feature natively. What you can do is to run 2 Sonar analysis:

  1. Your first analysis is the current one;
  2. The second analysis will consider the src/test/java as the "production" source code. Thus, this second analysis will give you the quality of your code. For this analysis, you can specify a specific Maven profile (or an alternative pom.xml) that will change the project information (for ex. it will indicate that src/test/java is the default sourceDirectory).