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 :
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 color, it means all those lines are covered under Junit testing.

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


If you see Yellow color, 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 
package com.blogspot.javasolutionsguide.eclmma.service;

public interface CalculatorService {
    int add(int a, int b);
    int subtract(int a, int b);
}
Here is the source code for  CalcualtorServiceImpl 
package com.blogspot.javasolutionsguide.eclemma.service.impl;

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 :
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;

@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));
    }

}




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 color 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 :
@Test
public void subtract_for_givenTwoInteger_shouldReturn_differenceOfTwo(){
        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.

Also, if you like JavaSolutionsGuide and want to contribute, go ahead and

- Write an article
- Share with me at javasolutionsguide@gmail.com and
- See Your Article Getting Published on main Page of JavaSolutionsGuide with your name.