Home > Enterprise >  Code Coverage is 0% in Sonarqube for Mockito tests
Code Coverage is 0% in Sonarqube for Mockito tests

Time:10-25

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.60.20:9000/ 
-Dsonar.login=admin   -Dsonar.password=sonar'

I don't get any single error but the Coverage percentage is still 0% and the number of lines is not changing after adding my tests.

enter image description here

I'm starting to think that this may be due to to my project having two folders.

Folder 1: /src/main/{artefact-id}.. containing the entities, services ect..

Folder 2: /src/test/{artefact-id}.. containing the Mockito tests ..

I'm leaving below my mokito class and my Pom.xml file:

MockitoClass for tests:

@SpringBootTest(classes = AchatApplication.class)
@ExtendWith(MockitoExtension.class)
@Slf4j

public class StockServiceImplMock {

    @Mock(lenient = true)
    StockRepository stockRepository;
    
    @InjectMocks
    StockServiceImpl StockSer;

    Stock stock = new Stock(1L,"stock 8", 12, 5);
    
    List<Stock> listStocks = new ArrayList<Stock>() {
        {
            add(new Stock("stock 1", 1, 1));
            add(new Stock("stock 2", 1, 1));
            add(new Stock("stock 3", 1, 1));
        }
    };

    @Test
    
    void retrieveStock() {
        Mockito.when(stockRepository.findById(Mockito.anyLong())).thenReturn(Optional.of(stock));
        Stock stock1 = StockSer.retrieveStock(0L);
        Assertions.assertNotNull(stock1);
    }
}

Pom.xml:

<?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.uni.ls</groupId>
    <artifactId>achat</artifactId>
    <version>1.0</version>
    <name>achat</name>
    <description>project</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>
    </properties>

    <distributionManagement>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <url>http://192.168.60.20:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
        <repository>
            <id>nexus-releases</id>
            <url>http://192.168.60.20: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>
    </dependencies>
    <build>
        <plugins>

            <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.60.20: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>

CodePudding user response:

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

CodePudding user response:

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

  • Related