In this tutorial I will explain you how to create a Code coverage report for JUnit with Mockito for RESTful Spring Boot project. The following tech stack has used in this project:
Import Project into Eclipse
Import into Eclipse as “Existing Maven Project”
org.jacoco 0.7.7.201606060606 prepare-agent report prepare-package report jacoco-check check PACKAGE LINE COVEREDRATIO 0.70 Right click on the project select Coverage As → JUnit Test
- Spring Boot(1.4.2)
- Maven (3.3.9)
- Eclipse (Neon, 4.6.0)
- Java 8
- Packaging (JAR)
- JPA
- RESTful
- H2
- JUnit
- Mockito
- Hamcrest
- EclEmma (eclips Coverage Unit Testing Tool)
Import Project into Eclipse
Import into Eclipse as “Existing Maven Project”
Choose the root directory of the project generated (where the pom.xml file is located) and click on Finish.
It will display the following project structure.
Launching the Application from Eclipse
Right click on DemoApplication.java → Run As → Java Application
Testing RESTful calls
Get All Emp’s
GET Request → http://localhost:8080/emp/
Unit Testing
We have implemented the unit test logic into the Controller and Service layer. You can download code check it.
Code Coverage
We have used a very useful Eclipse plugin called EclEmma to generate the code coverage report for Unit test case. code coverage will show you the exactly how much percentage of actual code is covered by your unit test cases.
How to install EclEmma plugin.
- In Eclipse go to “Help” menu
- Open Eclipse Market
- Search for “EclEmma”
- Install it
How to Configuration Code Coverage into pom.xml.
Below line of code you need to add inside tag.
jacoco-maven-plugin
Code Coverage Output
Class Level Output
Code Coverage Report int Dashboard
Below report you can find under target/site/jacoco/index.html
Note: out-of-the-box Spring Boot will setup the database (H2) configurations.
This post first appeared on Java Programming Tutorials Online For Java Coder, please read the originial post: here