{
"$type": "site.standard.document",
"path": "/java-code-coverage-with-gradle-and-jacoco-2/",
"publishedAt": "2016-01-22T21:22:04.000Z",
"site": "at://did:plc:6gssgguzeecdttuw4gpdshg2/site.standard.publication/self",
"tags": [
"coverage",
"gradle",
"java",
"coveralls-io"
],
"textContent": "There are so many great metrics to help you improve a project's quality. One of these metrics is code coverage.\n\nIn this post we will go over why this measurement is important, how to measure coverage using JaCoCo. I'll also wrap up with some info on coveralls.io which allows you to mesure this metric between builds.\n\nWhat is code coverage?\n\nCode coverage is a measure of how much for your application's code has been executed in testing. This covers not only seeing which lines of code have been executed, but also checking that all branches have been covered.\n\n[Embedded code (GitHub Gist)]\n\nThe a coverage tool will ensure your tests sent at least 3 cases:\n\n[Code block]\n\nWhy measure code coverage?\n\nThe more code that you have covered the lower the probability of potential bugs. Coverage is not a cure all for bug free code, but does force you to think about writing more tests.\n\nAfter adding coverage reports to several work projects, I can tell you all of a sudden every developer on the team seems to commit a few more tests. No one wants to be the one that commits a large amount of untested code.\n\nSetting up JaCoCo\n\nI'll add coverage to a simple spring-boot web application. I have a pre-made simple boot app on git hub: https://github.com/brookslyrette/spring-boot-demo-starter. You can use this as a starting point. First download the code or clone the git repo git clone https://github.com/brookslyrette/spring-boot-demo-starter.git\n\nYou do not have to to add much to build.gradle to generate a JaCoCo report.\n\n[Embedded code (GitHub Gist)]\n\nAll you need to do to run your tests and generate a coverage report is gradle test jacocoTestReport\n\n[Image: Screen-Shot-2016-01-21-at-4-32-52-PM.png]\n\nYou can see I don't have proper coverage! I better add some more tests. Here is a small test for the DemoController\n\n[Embedded code (GitHub Gist)]\n\nNow if we run our coverage results again, we get a nice improvement:\n\n[Image: Screen-Shot-2016-01-21-at-4-37-43-PM.png]\n\nYou can see the final product here in GitHub: https://github.com/brookslyrette/gradle-jacoco-coverage\n\nCoveralls.io\n\nWhile having coverage reports for the current code is great, it's even more useful to track changes in coverage over time. I stumbled onto a great tool for this http://coveralls.io.\n\nIt's free for any open source projects on GitHub or BitBucket and reasonably priced for private projects.\n\nIt keeps track of how code coverage changes over each build. [Image: Screen-Shot-2016-01-21-at-4-43-52-PM.png]\n\nConfiguring this is as simple as adding this plugin to your build: https://github.com/kt3k/coveralls-gradle-plugin\n\nRead the original post with all embeds and interactive content at https://rants.broonix.ca/java-code-coverage-with-gradle-and-jacoco-2/",
"title": "Java: Code coverage with Gradle and JaCoCo"
}