Home » Java » Enterprise Java » How to integrate and use EclEmma plugin for having good Junit Coverage

About Gaurav Bhardwaj

Gaurav Bhardwaj
Gaurav has done Masters in Computer Applications(MCA) and is working in Software development field for more than 10 years in Java/J2EE technologies. He is currently working with one of top MNC. He has worked on various frameworks like Struts, Spring, Spring Boot, Angular JS, JSF, Velocity, iBatis, MyBatis, Hibernate, JUnit, Mockito, Dozzer. He likes to explore new technologies and share his thoughts by writing a technical blog. He is the founder of JavaSolutionsGuide.blogspot.com.

How to integrate and use EclEmma plugin for having good Junit Coverage

Hello Friends,

If writing good code is important,then it is equally important to write good Junit test cases which covers all the business logic.By writing Junit test cases which covers business logic thoroughly ,we actually make sure that individually each method of our code is working fine as per expectation and hence reduce the chances of getting bugs in the later phases of software development.

In this tutorial,we will see how we can use EclEmma plugin to check code coverage of our java code and hence can make sure that we have good coverage through Junit test cases.

Step 1 :

Go to following link :

https://www.eclemma.org/

Step 2 :

Click  on Installation -> Download option on left menu.

Step 3 :

Right click on top most(latest) link(At the time of writing this article, it is “eclemma-3.1.2.zip”) and save Zip on your machine.

Step 4 :

Extract the zip at the same location or location of your choice.

Step 5 :

Copy the extracted folder and go to eclipse folder(Where your eclipse is installed) and paste this extracted folder to the dropins folder within eclipse folder.

Step 6 :

Restart Eclipse.

Step 7 :

Right click on your project folder and select Coverage As-> JUnit Test and click Coverage button.It will run all your test cases and check for coverage.

Now because at this stage,we don’t have any test case in our code for Service layer,we are getting 0.0% coverage and all code lines are shown red which means these lines are not covered under Junit test.

In general :

If you see Green colour, it means all those lines are covered under Junit testing.

If you see Red colour, it means all those lines are not covered under Junit testing.

If you see Yellow colour, it means your tests have run through those lines but not all test cases have been covered.

Example 

What we are going to do next is,we will create a Spring boot project,add some code in it and then gradually write test cases and see how EclEmma plugin gives as code coverage.

Step 8 : 

Let us look at it how it works by creating a simple Spring boot project with name “eclemma”.Please  follow my tutorial How to create spring boot project with Spring Initializer to create spring boot project.

Step 9:

In  newly created project,create packages  and classes with names as per following screenshot

Step 10 :

Here is the source code for CalculatorService 

01
02
03
04
05
06
07
08
09
10
11
12
package com.blogspot.javasolutionsguide.eclmma.service;
 
/**
 * @author JavaSolutionsGuide
 *
 */
public interface CalculatorService {
 
 int add(int a , int b);
  
 int subtract(int a,int b);
}
1
<br>
1
<br>
1
<br>

Here is the source code for  CalcualtorServiceImpl 

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
package com.blogspot.javasolutionsguide.eclemma.service.impl;
 
/**
 * @author javaSolutionsGuide
 *
 */
public class CalculatorServiceImpl {
  
 public int add(int a , int b) {
  int c = a + b;
  return c;
 }
 
 public int subtract(int a,int b) {
  int c = a - b;
  return c;
 }
}

Step 11 :

So ,first we will write test cases only for add() method and then run Eclemma coverage

Here is the code for JUnit test cases for add() method :

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.blogspot.javasolutionsguide.eclEmma.service.impl;
 
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
 
import com.blogspot.javasolutionsguide.eclemma.service.impl.CalculatorServiceImpl;
 
 
/**
 * @author JavaSolutionsGuide
 *
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {CalculatorServiceImpl.class})
public class CalculatorServiceImplTest {
  
 @Autowired
 private CalculatorServiceImpl calculatorService;
   
 @Test
 public void add_for_givenTwoInteger_shouldReturn_sumOfTwo() {
  Assert.assertEquals("Test case to check if add returns sum of two give positive numbers", 3, calculatorService.add(1, 2));
    }
  
}
1
<br>
1
<br>
1
<br>

Step 12 :

Now let us run the EclEmma Coverage again and see the results :

As we can see,the coverage percentage has increased from 0.0% to 60% for CalculatorServiceImpl class and also the lines of code for add() method has now green colour after running coverage,which means that these lines are covered by Junit tests.

Step 13 :

Similarly ,we will write all test cases for subtract method and see coverage.

Add following test case in  CalculatorServiceImplTest.java :

1
2
@Test
public void subtract_for_givenTwoInteger_shouldReturn_differenceOfTwo() {
1
2
Assert.assertEquals("Test case to check if subtract returns sum of two give positive numbers", 1, calculatorService.subtract(2, 1));
}

As we can see now,coverage is 100% for CalculatorServiceImpl class.

Summary 

So in this tutorial,we learnt how we can use EclEmma plugin to see the Junit test coverage of your code.On the basis of the Junit Code coverage report,we can find out which part of our code is covered by Junit and which is still left and then we can accordingly write more test cases for the uncovered lines of code,which will make sure that we have covered all required lines of code through Junit and hence result in better code quality. 

If you like reading this article,share it with your friends,colleagues for whom you think it might be helpful.

Published on Java Code Geeks with permission by Gaurav Bhardwaj, partner at our JCG program. See the original article here: How to integrate and use EclEmma plugin for having good Junit Coverage

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of