Sonar Scanner not analyzing src/test/java folders

1,823 views
Skip to first unread message

dead...@gmail.com

unread,
Mar 21, 2018, 3:20:20 PM3/21/18
to SonarQube
I downloaded SonarQube 7.0, and setup the Maven project to use Sonar scanner. The pom.xml looks like the following:

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.github.jitpack</groupId>
<artifactId>maven-simple</artifactId>
<version>0.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Simple Maven example</name>
<url>https://jitpack.io/#jitpack/maven-simple/0.1</url>

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <mavenSurefireVersion>2.20</mavenSurefireVersion>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <src.dir>src/main/java</src.dir>
    <sonar.sources>pom.xml,src/main/java</sonar.sources>
    <mavenSurefireVersion>2.20</mavenSurefireVersion>
</properties>

<dependencies>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<profiles>
    <profile>
        <id>sonarLocal</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <sonar.host.url>http://localhost:9000</sonar.host.url>
        </properties>
    </profile>
</profiles>

<build>
    <sourceDirectory>${src.dir}</sourceDirectory>
    <testResources>
        <testResource>
            <directory>${project.basedir}</directory>
            <filtering>true</filtering>
        </testResource>
    </testResources>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>${maven.compiler.source}</source>
                <target>${maven.compiler.source}</target>
                <encoding>${project.build.sourceEncoding}</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
                <encoding>${project.build.sourceEncoding}</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <id>validate</id>
                    <phase>validate</phase>
                    <configuration>
                        <includeTestSourceDirectory>true</includeTestSourceDirectory>
                        <configLocation>google_checks.xml</configLocation>
                        <encoding>${project.build.sourceEncoding}</encoding>
                        <consoleOutput>true</consoleOutput>
                        <failsOnError>true</failsOnError>
                        <failOnViolation>true</failOnViolation>
                        <violationSeverity>warning</violationSeverity>
                    </configuration>
                    <goals>
                        <goal>check</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${mavenSurefireVersion}</version>
        </plugin>
        <plugin>
            <groupId>org.sonarsource.scanner.maven</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>3.4.0.905</version>
        </plugin>
    </plugins>
</build>

My structure of the project looks like below:

src/main/java --> Contains app code.

src/test/java --> Contains all test code.

I use the command, "mvn clean install sonar:sonar -PsonarLocal" to execute Sonar.

Now I observed that Sonar never analyzes all .java files in the src/test/java folder.

To prove this, I added an unused variable in one of the test classes, and Sonar didn't catch it. If I do the same thing in any file in the src/main/java folder, Sonar catches it.

I created a StackOverflow thread at https://stackoverflow.com/questions/49407642/sonarqube-java-sonar-scanner-not-analyzing-src-test-java-folders. G. Ann recommended that I create a thread for it here.

Is there something I'm missing?

dead...@gmail.com

unread,
Mar 21, 2018, 8:16:15 PM3/21/18
to SonarQube
Can someone from the SonarQube team please respond?

Michael Gumowski

unread,
Mar 22, 2018, 4:33:45 AM3/22/18
to dead...@gmail.com, SonarQube
Hello,

Like Ann already mentioned on your StackOverflow question, all the rules from SonarJava have an implicit scope. It means that each rules will target MAIN sources (from src/main/java with maven), or TEST sources (src/test/java). They currently can not be applied on both scope. On the other hand, a file can also only be assigned to one single scope. If you try to force your test sources as being main sources as well, you will end up with the problem you reported of files being indexed twice.

The immense majority of rules have been written to target main sources (and thought that way), such as the unused local variable rule (S1481) you are mentioning, and only a bunch of rules are currently targeting test sources. You can find them by searching for rules having the "tests" tag on your SonarQube instance: https://next.sonarqube.com/sonarqube/coding_rules?languages=java&tags=tests 

Now, things are moving in this direction and we are currently planning to allow (some/many of the) rules to be played against MAIN and TEST sources. This is however not implemented yet. See the following tickets for more details: SONARJAVA-2684MMF-1160.

In conclusion: thanks for your feedback, it's good to see that people are waiting for it, the feature is coming!

Regards,
Michael

--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/8797e96f-4e88-4c70-ae69-703cc1ef9eee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Michael Gumowski | SonarSource
Software Developer, Language Team
https://www.sonarsource.com

G. Ann Campbell

unread,
Mar 22, 2018, 1:05:01 PM3/22/18
to SonarQube
Hi,

For the record I recommended you create a thread here to discuss the problem you had trying to analyze your tests as source code. Since you're doing a Maven analysis, I guess your tests are automatically being picked up as tests, and then again as source code because of your -Dsonar.sources definition. You might want to try SonarQube Scanner for your tests-as-source analysis.


Ann
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages