Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Generate CodeCoverage Reports for JUnit test cases With Maven And EclEmma

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:

  • 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)
Download code from github  Spring Restful with Junit and Code coverage

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.
  1. In Eclipse go to “Help” menu
  2. Open Eclipse Market
  3. Search for “EclEmma”
  4. Install it

How to Configuration Code Coverage into pom.xml.

Below line of code you need to add inside tag.

     
                org.jacoco
                jacoco-maven-plugin
                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

    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

    Share the post

    Generate CodeCoverage Reports for JUnit test cases With Maven And EclEmma

    ×

    Subscribe to Java Programming Tutorials Online For Java Coder

    Get updates delivered right to your inbox!

    Thank you for your subscription

    ×