skip to Main Content

I developed some Mockito tests using Jenkins pipeline and will see the report through SonarQube.

So far this is what I typed in the Jenkins script:

sh '  mvn sonar:sonar -Dsonar.sources=src/main/java -Dsonar.css.node=. 
-Dsonar.java.binaries=. -Dsonar.host.url=http://192.168.2.2:9000/ 
-Dsonar.login=admin   -Dsonar.password=sonar -Dsonar.jacoco.reportPath=build/reports/jacoco.xml'

I’ve also added this command to the stage before creating the artifact to clean the project (so it ignores at first tests so it doesn’t generate the exception for ConnectionNeeded)

 sh 'mvn package -Dmaven.test.skip=true -P test-coverage'

enter image description here

I get an exception and the final stage (I believe this is because I don’t have a database on my Centos machine)
enter image description here

[ERROR] Tests run: 11, Failures: 0, Errors: 11, Skipped: 0

and the Coverage percentage on SonarQube is still 0% and the number of lines is not changing after adding my tests.

enter image description here


    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.5.3</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
        <groupId>uk.un.ts</groupId>
        <artifactId>proj</artifactId>
        <version>1.0</version>
        <name>achat</name>
        <description>Projet pour le module DevOps</description>
        <properties>
            <java.version>1.8</java.version>
            <maven-jar-plugin.version>3.1.0</maven-jar-plugin.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
             <jacoco.version>0.8.6</jacoco.version>
        <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
        <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
        <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
        <sonar.language>java</sonar.language>
        </properties>
    
        <distributionManagement>
            <snapshotRepository>
                <id>nexus-snapshots</id>
                <url>http://192.168.2.2:8081/repository/maven-snapshots/</url>
            </snapshotRepository>
            <repository>
                <id>nexus-releases</id>
                <url>http://192.168.2.2:8081/repository/maven-releases/ </url>
            </repository>
    
    
        </distributionManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-boot-starter</artifactId>
                <version>3.0.0</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>3.0.0</version>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter</artifactId>
                <version>5.9.1</version>
            </dependency>
    <dependency>
        <groupId>org.jacoco</groupId> 
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.6</version>
    </dependency>
        </dependencies>
        <build>
            <plugins>
    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>${jacoco.version}</version>
        <executions>
            <execution>
                <id>jacoco-initialize</id>
                <goals>
                    <goal>prepare-agent</goal>
                </goals>
            </execution>
            <execution>
                <id>jacoco-site</id>
                <phase>package</phase>
                <goals>
                    <goal>report</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>${maven-deploy-plugin.version}</version>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.sonatype.plugins</groupId>
                    <artifactId>nexus-staging-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>default-deploy</id>
                            <phase>deploy</phase>
                            <goals>
                                <goal>deploy</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <serverId>nexus</serverId>
                        <nexusUrl>http://192.168.2.2:8081/</nexusUrl>
                        <skipStaging>true</skipStaging>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                            </exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>

2

Answers


  1. I think it’s because of the name of the test class:

    By default Maven uses the following naming conventions when looking for tests to run:

    • Test*
    • *Test
    • *Tests (has been added in Maven Surefire Plugin 2.20)
    • *TestCase

    extracted from: Maven does not find JUnit tests to run

    Login or Signup to reply.
  2. I see 2 things:

    1. For code coverage reports you need an additional maven plugin, usually JaCoCo
      https://www.eclemma.org/jacoco/trunk/doc/maven.html
    2. Conventions
      Having the 2 folders is OK, and it is usually done like this, however the default naming convention is src/main/java/ for your application sources and src/main/test/ for test related sources.
      Usage of the {artifact-id} is more of a good practice (thumbs up) than part of the convention, so I’m glad you do it

    I would question if mvn test can pick up your tests at all (issue #2) and once you confirm this, then try to setup the JaCoCo plugin

    — Update 2022-10-27
    For JaCoCo setting you can setup the plugin as

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.7</version>
            <executions>
              <execution>
                <goals>
                  <goal>prepare-agent</goal>
                </goals>
              </execution>
              <execution>
                <id>report</id>
                <phase>test</phase>
                <goals>
                  <goal>report</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
    

    This will make it so when you run the test mvn goal the reports will be generated under ${buildDir}/site/jacoco

    As for linking it to sonar you can set he following property in your sh script -Dsonar.jacoco.reportPath=${project.basedir}/../target/site/jacoco/jacoco.xml

    Here, I am assuming your sonar instance is somewhat upgraded and prefers the XML JaCoCo report over the old binage .exec file 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search