I've been facing a really weird problem with a project and the use of SonarQube.
As you can see in the image below, my test coverage is 9.5% of my overall code which is pretty low regarding of the quantity of code I've been writing and will be in the future.
When I first tried to write tests, they were not detected, because I forgot a plugin inside my pom.xml, which I added and is the following
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>generate-code-coverage-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
After that, I got this window on SonarQube.
Nice ! My test are detected and I went to 13.2% on new code and 9.5% overall ! But that's where the real problem started, as I wrote more test and be pushing them to SonarQube, nothing more was shown. The percentage didn't increased, and the line that I thought the tests covered were not. And the weirdest part is that the test code was on SonarQube ! It was pushed to Sonar but not detected as test code or whatever it should be !
So, I tried to watch as many videos as I could but nothing seems to really work and I just had the feeling that I lost time. I've been trying to code simple test classes on my simplest classes, for example : Here is my class : Categorie.java
package com.back.projetbdi_groupe1.Entities;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@Entity
public class Categorie {
@Id
private String idCategorie;
private String libCateg;
public String getIdCategorie() {
return idCategorie;
}
public void setIdCategorie(String idCategorie) {
this.idCategorie = idCategorie;
}
public String getLibCateg() {
return libCateg;
}
public void setLibCateg(String libCateg) {
this.libCateg = libCateg;
}
}
And my test class : CategorieTest.java
package com.back.projetbdi_groupe1.entities;
import com.back.projetbdi_groupe1.Entities.Categorie;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CategorieTest {
@Test
public void testGetIdCategorie(){
Categorie categorie = new Categorie();
categorie.setIdCategorie("1");
assertEquals("1",categorie.getIdCategorie());
}
@Test
public void testGetLibCateg(){
Categorie categorie = new Categorie();
categorie.setLibCateg("categ");
assertEquals("categ",categorie.getLibCateg());
}
}
But : You can see that nothing is covered. So, I wanted to know if I'm not testing the right way, or is it a SonarQube bug ? Or my pom.xml is incomplete ? I will put what I found useful about the pom.xml below. Oh, and to " push " to SonarQube the code, I'm using the following command : mvn clean verify sonar:sonar -Dsonar.login=MyFabulousAndLongToken
Rest of the pom.xml :
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<jettyVersion>9.4.3.v20170317</jettyVersion>
<jettyServletVersion>9.4.3.v20170317</jettyServletVersion>
<sonar.host.url>http://im2ag-sonar.u-ga.fr:9000/</sonar.host.url>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.5.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
CodePudding user response:
I don't have a definitive answer, just some things that should be checked.
It's a little odd to use the "clean" goal on the same mvn command line you run "sonar:sonar", but it's possible that can work. Inspect the output from this command and verify that it's running all of your unit tests.
When Jacoco and Surefire are properly integrated, it should generate an html page you can visit to inspect the Jacoco results. It might be in "target/jacoco_report". If that is there (it's possible it might be in a different subdirectory), then view that in your browser and see if there are tests missing from that report. SonarQube only integrates the jacoco data that is produced.
In the SonarQube results, you can also see the list of unit tests that are executed. See if "CategorieTest" is in that list.
CodePudding user response:
So, to resolve my own problem I did nothing but testing my code "less wrongly" if I could say.
That's what the previous example of testing looks like now !
package com.back.projetbdi_groupe1.Entities;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
class CategorieTest {
/**
* Methods under test:
*
* <ul>
* <li>default or parameterless constructor of {@link Categorie}
* <li>{@link Categorie#setIdCategorie(String)}
* <li>{@link Categorie#setLibCateg(String)}
* <li>{@link Categorie#getIdCategorie()}
* <li>{@link Categorie#getLibCateg()}
* </ul>
*/
static Categorie actualCategorie;
@BeforeAll
public static void init() {
actualCategorie = new Categorie();
actualCategorie.setIdCategorie("1");
actualCategorie.setLibCateg("Lib Categ");
}
@Test
void testGetIdCategorie() {
assertEquals("1", actualCategorie.getIdCategorie());
}
@Test
void testGetLibCateg() {
assertEquals("Lib Categ", actualCategorie.getLibCateg());
}
}
Everything is green, im H A P P Y.