How to run Coverage on SonarCloud using Maven Dependency instead of Source Code

573 Views Asked by At

I'm writing some Test Cases for Apache JCS. For University purposes i need to integrate these tests with Maven and SonarCloud, in particular i need to run tests without having in local the JCS source code, but "importing" the project via Maven Dependency.

Configuring the pom.xml and the sonar-project.properties the project build correctly and the Test Cases are correctly executed. However, i need also the Coverage metric, but it's stuck to 0%.

I think that the solution lies in configuring the pom.xml so that coverage runs not only on local code, but also by resolving dependencies. Any Idea?

pom.xml

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>danilo.dellorco</groupId>
<artifactId>jcsTests</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>jcsTests</name>
<url>http://maven.apache.org</url>

<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>
</properties>

<dependencies>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.1</version>
    </dependency>

    <dependency>
        <groupId>jcs</groupId>
        <artifactId>jcs</artifactId>
        <version>1.3</version>

        <!-- Remove bug of jdbc-sdtext error -->
        <exclusions>
            <exclusion>
                <artifactId>jdbc-stdext</artifactId>
                <groupId>javax.sql</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>

</dependencies>

<build>
    <!-- Specifico la cartella dove si trovano i Test JUnit -->
    <testSourceDirectory>src/test/</testSourceDirectory>
    
    <!-- Dichiaro la cartella contenente i file di configurazioni dei test -->
    <sourceDirectory>src/test/resources</sourceDirectory>

    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit4</artifactId>
                        <version>2.22.0</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

sonar-project.properties

sonar.projectKey=danilo-dellorco_jcstests
sonar.projectName=jcsTests
sonar.links.homepage=https://github.com/danilo-dellorco/jcsTests
sonar.links.ci=https://travis-ci.com/github/danilo-dellorco/jcsTests
sonar.links.scm=https://github.com/danilo-dellorco/jcsTests
sonar.links.issue=https://github.com/danilo-dellorco/jcsTests/issues

sonar.host.url=https://sonarcloud.io
sonar.organization=danilo-dellorco
sonar.sources=src
sonar.language=java

sonar.java.source=13
sonar.java.binaries=.

# Debug
sonar.verbose=true

travis.yml

language: java
jdk:
  - openjdk13
os: 
  - linux
dist:
  - debian
addons:
  sonarcloud:
    organization: "danilo-dellorco"
    token: "##############################"
script: 
  # This command analyzes only the pom.xml file
  #- mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install sonar:sonar -Dsonar.projectKey=danilo-dellorco_jcsTests

  - mvn test
  - sonar-scanner

Test Example

import org.apache.jcs.JCS;
import org.apache.jcs.access.exception.CacheException;
...


@RunWith(Parameterized.class)
@Category(JUnitTest.class)
public class JCSRemovalSimpleConcurrentTest {
    private int count;
    private static JCS jcs;
    
    public JCSRemovalSimpleConcurrentTest(int count){
        this.count = count;
    }

    @BeforeClass
    public static void configure() throws CacheException {
        JCS.setConfigFilename("/TestRemoval.ccf");
        jcs = JCS.getInstance("testCache1");
    }

    ...
0

There are 0 best solutions below