我正在为 Apache JCS 编写一些测试用例。出于大学目的,我需要将这些测试与 Maven 和 SonarCloud 集成,特别是我需要在没有本地 JCS 源代码的情况下运行测试,而是通过 Maven 依赖项“导入”项目。
正确配置pom.xml
和sonar-project.properties
项目构建并且正确执行测试用例。但是,我还需要覆盖率指标,但它一直保持在 0%。
我认为解决方案在于配置,pom.xml
以便覆盖不仅在本地代码上运行,而且还通过解决依赖关系来运行。任何想法?
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>
声纳-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
测试示例
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");
}
...