Search code examples
javamaventestingjunitcobertura

Why my tests aren't run when I use 'mvn cobertura:cobertura'?


I'm trying to run one test for my class "Sinus" (used to compute the sinus of a float), but when I try to run this test to generate my coverage report with Cobertura, it doesn't work and I really don't know why ! Dou you have advices or any explanation please ? (I use the cmd : mvn cobertura:cobertura)

-This is my test:

import static org.junit.Assert.*;
import org.junit.Test;


public class SinusTest {

Sinus test = new Sinus();

    @Test
    public static void Sinuszero() {
        Sinus test = new Sinus();
        assertTrue(test.sin(23) == 5);
    }
}

And there is the result :

[INFO] 
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ 2 ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.193 s
[INFO] Finished at: 2021-05-26T11:07:45+02:00

So what's the problem ? Any ideas ? There is my pom.xml below :

4.0.0
<groupId>1</groupId>
<artifactId>2</artifactId>
<version>2</version>
<packaging>jar</packaging>

<name>2</name>
<description>Blank project for Vanilla Spring WebFlux.fn</description>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.RELEASE</version> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <start-class>2.App</start-class>
    <spring-fu.version>0.0.3.BUILD-SNAPSHOT</spring-fu.version>
</properties>


<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
        <exclusions>
            <exclusion>
                <groupId>javax.annotation</groupId>
                <artifactId>javax.annotation-api</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.hibernate.validator</groupId>
                <artifactId>hibernate-validator</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.fu</groupId>
        <artifactId>spring-fu-jafu</artifactId>
        <version>${spring-fu.version}</version>
    </dependency>
    <dependency>
        <groupId>am.ik.yavi</groupId>
        <artifactId>yavi</artifactId>
        <version>0.0.18</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <scope>test</scope> 
    </dependency>   
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>

    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        
        <plugin>
        <groupId>org.codehaus.mojo</groupId>
         <artifactId>cobertura-maven-plugin</artifactId>
         <version>2.5.1</version>
         <configuration>
         <formats>
            <format>html</format>
            <format>xml</format>
         </formats>
         </configuration>
       </plugin>
       
       <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M5</version>
       </plugin>
     
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
        </snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
        </snapshots>
    </pluginRepository>
    <pluginRepository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

Solution

  • Your test is a junit4-api based. But from your pom.xml you have junit5 dependencies.

    Removing jupiter dependencies should do the trick.

    Regarding cobertura, as you run on java 8 preferably you should migrate to JaCoCo as cobertura with java version higher than 7 is buggy.