0

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>

Compte Gmail
  • 137
  • 3
  • 8

2 Answers2

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

fneira
  • 291
  • 1
  • 8
  • Hi this caused me an error in my pipeline. I got the Connection Failed exception. So I opened Camp (my local host) and I still get that error. But based on the console output, it really considers it as a test class but I believe it is being considered as Junit and not Mockito which is why it was asking me to open the database. I'll try to add a plugin like the solution above stated. – Compte Gmail Oct 25 '22 at 11:33
  • So I tried to add the Jacoco plugin and it's also not working (Just updated my code in the question so you could have a look) – Compte Gmail Oct 25 '22 at 12:16
1

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 :)

Jhilton
  • 420
  • 3
  • 7
  • Hi again , I did what you asked me and it doesn't seem to work. I edited my question where I just added the plugin so you could have a look. I honestly don't know what version I should use for JaCoCo – Compte Gmail Oct 25 '22 at 12:10
  • 1
    @CompteGmail I have updated the answer, let me know if you still have some issues. Again, Im not completely sure your tests are being picked up at all, does the test goal tells you how many tests were executed ? – Jhilton Oct 27 '22 at 08:21
  • Hey @Jhilton I just tried what you told me. I tried different methods of plugin of Sonar and still not working. I updated my answer and pom.xml. The exception that i'm getting is that he's still not reading my tests and forcing me to have database connection – Compte Gmail Oct 27 '22 at 16:55
  • @CompteGmail looks like the issue is not longer your sonar setup but your tests, all 11 tests are failing, I am guessing before any code in `/src/main/java` gets executed, therefore the 0% coverage. So instead, it would be better to get a look into the Unit Test class. Do you use Spring test framework, or do you test the class as a Plain Class? maybe create a simple "sure fire" class with a method that return true and a unit test for it. that should give you coverage for that single class – Jhilton Oct 27 '22 at 23:25