Skip to content

Commit 62a045c

Browse files
committed
Add a test and instructions to generate a jacoco report file
1 parent 20abbca commit 62a045c

File tree

6 files changed

+103
-89
lines changed

6 files changed

+103
-89
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# coverage-example-java
2+
3+
Example repository on how to generate a coverage report for Java.
4+
5+
## Requirements
6+
7+
- Maven 3.x
8+
- Java 8
9+
10+
If you don't have those specific versions, [SDKMAN!](https://sdkman.io/install) can be helpful.
11+
12+
## Run tests
13+
14+
```bash
15+
mvn clean test
16+
```
17+
18+
Running tests will also generate a [JaCoCo](https://www.eclemma.org/jacoco/) report. After successfully running the
19+
tests, you can find the XML report at `target/site/jacoco/jacoco.xml`.

pom.xml

Lines changed: 42 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,51 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

33
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5-
<modelVersion>4.0.0</modelVersion>
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
66

7-
<groupId>com.codacy.app</groupId>
8-
<artifactId>coverage-example-java</artifactId>
9-
<version>1.0-SNAPSHOT</version>
7+
<groupId>com.codacy.app</groupId>
8+
<artifactId>coverage-example-java</artifactId>
9+
<version>1.0-SNAPSHOT</version>
1010

11-
<name>coverage-example-java</name>
11+
<name>coverage-example-java</name>
1212

13-
<properties>
14-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15-
<maven.compiler.source>1.7</maven.compiler.source>
16-
<maven.compiler.target>1.7</maven.compiler.target>
17-
</properties>
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<maven.compiler.source>1.8</maven.compiler.source>
16+
<maven.compiler.target>1.8</maven.compiler.target>
17+
</properties>
1818

19-
<dependencies>
20-
<dependency>
21-
<groupId>junit</groupId>
22-
<artifactId>junit</artifactId>
23-
<version>4.11</version>
24-
<scope>test</scope>
25-
</dependency>
26-
</dependencies>
19+
<dependencies>
20+
<dependency>
21+
<groupId>junit</groupId>
22+
<artifactId>junit</artifactId>
23+
<version>4.11</version>
24+
<scope>test</scope>
25+
</dependency>
26+
</dependencies>
2727

28-
<build>
29-
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
30-
<plugins>
31-
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
32-
<plugin>
33-
<artifactId>maven-clean-plugin</artifactId>
34-
<version>3.1.0</version>
35-
</plugin>
36-
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
37-
<plugin>
38-
<artifactId>maven-resources-plugin</artifactId>
39-
<version>3.0.2</version>
40-
</plugin>
41-
<plugin>
42-
<artifactId>maven-compiler-plugin</artifactId>
43-
<version>3.8.0</version>
44-
</plugin>
45-
<plugin>
46-
<artifactId>maven-surefire-plugin</artifactId>
47-
<version>2.22.1</version>
48-
</plugin>
49-
<plugin>
50-
<artifactId>maven-jar-plugin</artifactId>
51-
<version>3.0.2</version>
52-
</plugin>
53-
<plugin>
54-
<artifactId>maven-install-plugin</artifactId>
55-
<version>2.5.2</version>
56-
</plugin>
57-
<plugin>
58-
<artifactId>maven-deploy-plugin</artifactId>
59-
<version>2.8.2</version>
60-
</plugin>
61-
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
62-
<plugin>
63-
<artifactId>maven-site-plugin</artifactId>
64-
<version>3.7.1</version>
65-
</plugin>
66-
<plugin>
67-
<artifactId>maven-project-info-reports-plugin</artifactId>
68-
<version>3.0.0</version>
69-
</plugin>
70-
</plugins>
71-
</pluginManagement>
72-
</build>
28+
<build>
29+
<plugins>
30+
<plugin>
31+
<groupId>org.jacoco</groupId>
32+
<artifactId>jacoco-maven-plugin</artifactId>
33+
<version>0.8.7</version>
34+
<executions>
35+
<execution>
36+
<goals>
37+
<goal>prepare-agent</goal>
38+
</goals>
39+
</execution>
40+
<execution>
41+
<id>report</id>
42+
<phase>test</phase>
43+
<goals>
44+
<goal>report</goal>
45+
</goals>
46+
</execution>
47+
</executions>
48+
</plugin>
49+
</plugins>
50+
</build>
7351
</project>

src/main/java/com/codacy/app/App.java

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.codacy.utils;
2+
3+
public class Math {
4+
5+
private int magicNumber;
6+
7+
public Math(int magicNumber) {
8+
this.magicNumber = magicNumber;
9+
}
10+
11+
/**
12+
* Adds 2 numbers, unless there is a magic number on the second argument
13+
*/
14+
public int magicAdd(int x, int y) {
15+
if (y == magicNumber) {
16+
return y - x;
17+
} else {
18+
return x + y;
19+
}
20+
}
21+
}

src/test/java/com/codacy/app/AppTest.java

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.codacy.utils;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class MathTest {
8+
@Test
9+
public void shouldAddNumbers() {
10+
Math math = new Math(23);
11+
12+
assertEquals(7, math.magicAdd(3, 4));
13+
}
14+
15+
// Uncomment this to have 100% coverage
16+
// @Test
17+
// public void shouldSubtractIfMagicNumber() {
18+
// Math math = new Math(4);
19+
// assertEquals(1, math.magicAdd(3, 4));
20+
// }
21+
}

0 commit comments

Comments
 (0)