Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shankybnl/junit-jacoco-example
This tutorial is for beginners to set up jacoco in gradle project and get the code coverage of unit tests
https://github.com/shankybnl/junit-jacoco-example
code-coverage coverage-report gradle jacoco java junit unit-testing
Last synced: 3 months ago
JSON representation
This tutorial is for beginners to set up jacoco in gradle project and get the code coverage of unit tests
- Host: GitHub
- URL: https://github.com/shankybnl/junit-jacoco-example
- Owner: shankybnl
- Created: 2017-07-03T08:25:02.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-07-03T09:22:56.000Z (over 7 years ago)
- Last Synced: 2024-10-11T03:11:20.753Z (4 months ago)
- Topics: code-coverage, coverage-report, gradle, jacoco, java, junit, unit-testing
- Language: HTML
- Homepage:
- Size: 540 KB
- Stars: 4
- Watchers: 1
- Forks: 11
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Java gradle project with Junit and JaCoCo example
*Pre-requisite: gradle and java is installed on your machine.*
1. Create a gradle project (I used Eclipse IDE)
![Image](https://github.com/shankybnl/junit-jacoco-example/blob/master/images/1.png)
2. Inside the project you will see two folders **main** and **test** under src folder. Under main you write your development code and under test you write your unit (Junit) tests and integration tests.
![Image](https://github.com/shankybnl/junit-jacoco-example/blob/master/images/2.png)
3. Add the below lines of code to your **build.gradle** file present in the root directory.
```java
apply plugin: "jacoco"
``````java
jacocoTestReport{
reports {
xml.enabled false
csv.enabled false
html.destination "${buildDir}/jacocoHtml"
}
}
```If Junit dependency is not present then add it:
```java
dependencies {
testCompile 'junit:junit:4.12'
}
```3. Create a **Example.java** class under main folder.
```java
public class Example {
public long someRandomMethod(int a, int b) {
if (a > 10 && b > 10){
return a+b;
}
else{
return a-b;
}
}
}
```
4. To verify this method, we will be writing Junit test under test folder with name **ExampleTest.java**
*@Test* annontation tells Junit that it is a test method to execute.```java
public class ExampleTest {@Test
public void verifySomeRandomMethod() {
Example junitTest = new Example();
junitTest.someRandomMethod(11, 12);
}
}
```
5. Go to the root directory of your project. And execute the below command to execute Junit test.```java
gradle clean test
```6. Junit results report will be generated /JunitJacocoExample/build/reports/tests/test/classes/ExampleTest.html
![Image](https://github.com/shankybnl/junit-jacoco-example/blob/master/images/5.png)
7. To generate code coverage report, execute the below command.
```java
gradle jacocoTestReport
```8. Code coverage report will be generated /JunitJacocoExample/build/jacocoHtml/index.html
![Image](https://github.com/shankybnl/junit-jacoco-example/blob/master/images/4.png)
Code coverage at class level can be verified by navigating to **Example.java**
![Image](https://github.com/shankybnl/junit-jacoco-example/blob/master/images/3.png)
Clone/download the working project from https://github.com/shankybnl/junit-jacoco-example