{"id":16372404,"url":"https://github.com/shankybnl/junit-jacoco-example","last_synced_at":"2025-04-15T11:31:43.964Z","repository":{"id":170415225,"uuid":"96087808","full_name":"shankybnl/junit-jacoco-example","owner":"shankybnl","description":"This tutorial is for beginners to set up jacoco in gradle project and get the code coverage of unit tests","archived":false,"fork":false,"pushed_at":"2017-07-03T09:22:56.000Z","size":553,"stargazers_count":4,"open_issues_count":0,"forks_count":11,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-28T20:39:48.750Z","etag":null,"topics":["code-coverage","coverage-report","gradle","jacoco","java","junit","unit-testing"],"latest_commit_sha":null,"homepage":"","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shankybnl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-07-03T08:25:02.000Z","updated_at":"2023-06-15T09:07:37.000Z","dependencies_parsed_at":null,"dependency_job_id":"d06804e8-01a0-45ec-b674-8b123c2eb477","html_url":"https://github.com/shankybnl/junit-jacoco-example","commit_stats":null,"previous_names":["shankybnl/junit-jacoco-example"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shankybnl%2Fjunit-jacoco-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shankybnl%2Fjunit-jacoco-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shankybnl%2Fjunit-jacoco-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shankybnl%2Fjunit-jacoco-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shankybnl","download_url":"https://codeload.github.com/shankybnl/junit-jacoco-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249061053,"owners_count":21206444,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["code-coverage","coverage-report","gradle","jacoco","java","junit","unit-testing"],"created_at":"2024-10-11T03:11:17.091Z","updated_at":"2025-04-15T11:31:43.698Z","avatar_url":"https://github.com/shankybnl.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"#  Java gradle project with Junit and JaCoCo example\n\n*Pre-requisite: gradle and java is installed on your machine.*\n\n1. Create a gradle project (I used Eclipse IDE)\n\n![Image](https://github.com/shankybnl/junit-jacoco-example/blob/master/images/1.png)\n\n2. 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.\n\n![Image](https://github.com/shankybnl/junit-jacoco-example/blob/master/images/2.png)\n\n3. Add the below lines of code to your **build.gradle** file present in the root directory.\n\n```java\napply plugin: \"jacoco\" \n```\n\n```java \njacocoTestReport{\n    reports {\n            xml.enabled false\n            csv.enabled false\n            html.destination \"${buildDir}/jacocoHtml\"\n    }\n}\n```\n\nIf Junit dependency is not present then add it:\n\n```java\ndependencies {\n     testCompile 'junit:junit:4.12'\n}\n```\n\n3. Create a **Example.java** class under main folder.\n\n```java\npublic class Example {\n\t\n\t public long someRandomMethod(int a, int b) {\n\t    \t\n\t    \tif (a \u003e 10 \u0026\u0026 b \u003e 10){\n\t    \t\treturn a+b;\n\t    \t}\n\t    \telse{\n\t    \t\treturn a-b;\n\t    \t}\n\t       \n\t    }\n}\n```\n4. To verify this method, we will be writing Junit test under test folder with name **ExampleTest.java**\n    *@Test* annontation tells Junit that it is a test method to execute.\n\n```java\npublic class ExampleTest {\n\n    @Test\n\tpublic void verifySomeRandomMethod() {\n\t\tExample junitTest = new Example();\n\t\tjunitTest.someRandomMethod(11, 12);\n\t}\n}\n```\n5. Go to the root directory of your project. And execute the below command to execute Junit test.\n\n```java\ngradle clean test\n```\n\n6. Junit results report will be generated /JunitJacocoExample/build/reports/tests/test/classes/ExampleTest.html\n\n![Image](https://github.com/shankybnl/junit-jacoco-example/blob/master/images/5.png)\n\n\n7. To generate code coverage report, execute the below command.\n\n```java\ngradle jacocoTestReport\n```\n\n8. Code coverage report will be generated /JunitJacocoExample/build/jacocoHtml/index.html\n\n![Image](https://github.com/shankybnl/junit-jacoco-example/blob/master/images/4.png)\n\nCode coverage at class level can be verified by navigating to **Example.java**\n\n![Image](https://github.com/shankybnl/junit-jacoco-example/blob/master/images/3.png)\n\n\n\nClone/download the working project from https://github.com/shankybnl/junit-jacoco-example\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshankybnl%2Fjunit-jacoco-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshankybnl%2Fjunit-jacoco-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshankybnl%2Fjunit-jacoco-example/lists"}