Agility, Java programming, New technologies and more…
  • rss
  • Home
  • Management
  • Agile Programming
  • Technology
  • Linux
  • Event
  • Android app
  • Contact
  • About the author
  • English
  • Francais

EclEmma – Do you need a good cover for this winter ?

Fabian Piau | Wednesday November 11th, 2009 - 10:23 PM
  • Print
  • Twitter
  • LinkedIn
  • Facebook
  • Pocket

 Version française disponible

Note
The official EclEmma website references this article in the “Blog Entries About EclEmma” category.

Improve your code coverage with Emma

In this new article, I introduce you the EclEmma plug-in (contraction of Eclipse & Emma), Emma is a free code coverage tool for the Java programming language. Of course, EclEmma is its porting under Eclipse.

I am using it since several weeks now and… I am wondering how I manage without it until now.


Plug-in installation

From your Eclipse menu, select Help -> Install New Software… -> Add site. The update site is http://update.eclemma.org/. To complete and finish the installation, Eclipse has to be restarted.


How to use this software ?

To run your unit-tests, you right-click on a package, a test suite or a test case, in the menu select Run As -> JUnit Test.

To run your test(s) with EclEmma in order to check the coverage rate, what you have to do is very similar. You only have to use the new submenu “Coverage As” instead of “Run As”. You can note that it is possible to use the coverage launcher button in the toolbar (Java perspective), but I’d prefer the menu.

Coverage launcher button

Coverage launcher button


Full presentation

Here is an example, step by step, to see how it is working. In the same time, we are going to talk about Test-driven Development.

So, we want to implement the following functionnality: the conversion of a raw text into a properly formated HTML text. The example will be based on the french language because we use much more special characters. This is a classical & easy stuff to do, but just perfect to present EclEmma.

Let’s start by writting our first test to manage the accents management (é, è, à, ù and so on) .

Our first test

Our first test

At this stage, the test is not compiling at all. Let’s implement just the code we need in order our test successfully compile.


After creating the “ConvertToHtml” class and its “convert” method, we can run the test.

The code needed for our test to compile

The code needed for our test to compile


Like we expected, test is failing.

A failing test

A failing test


Let’s implement the “convert” method.

The code needed to pass the test

The code needed to pass the test


Now, test is passing. Enjoy the green light!

Test is passing

Test is passing


But the implemented code is not perfect, it needs some refactoring.

Code refactoring

Code refactoring

Let’s check out if the test is still passing. Yes, it does.

At this point, you can wonder: Ok, I know that my code is enough to pass the test. But, how to be sure I haven’t written too much code. Some lines of code I have never tested in my test…

The solution is obviously EclEmma. So, we run the test using the coverage checker in EclEmma.

Our code is fully covered

Our code is fully covered


The covered code is highlighted in green. In this case, our code in fully tested.

The test is covered too

The test is covered too


Moreover, you can access to the coverage view. The coverage rate is 100%. Great !

The EclEmma coverage view

The EclEmma coverage view


I have to admit that, on this kind of example, this is not so difficult to get the highest rate. But, keep in mind, the usefulness of such tool when you are working on a big project whith several hundred thousand lines of code and thousand of tests.

For this online tutorial, I’m going to do some bad stuff and go against TDD principles. Let’s bring some modifications to the application code without having written a new test.

New piece of code

New piece of code


And rerun the test with EclEmma.

The code is not fully covered anymore

The code is not fully covered anymore

The coverage rate is now at 95%. EclEmma shows us that what we added is clearly a not-tested code. The corresponding lines of code are highlighted in red. Thanks to EclEmma, we get the feedback that the test is not complete.


After a glance at the coverage view, I know exactly which files are concerned. That’s not as funny as it has to be, because I have only two files in my project.

The EclEmma coverage view

The EclEmma coverage view


To continue the example, we can add a new test to test the successful no-conversion of a text which has not to be converted in HTML format.

A second test

A second test

I benefit from the time spent to think how to built my test to improve my code by deleting the prefix in the output text, and extract it as a constant.


I use EclEmma again and… We are back to a 100% coverage rate, all is successfully tested, and my code do exactly what I want (no more and no less).

Back to an optimal coverage

Back to an optimal coverage


Once again, and for teaching (of course), we modify the application code without modifying or creating a test. Now we suppose we use a no-conversion suffix and not only a prefix. We run tests again. Oh, nice colors !

A partly covered line

A partly covered line


A line of code is highlighted in yellow, it means this line is partly tested. We can arrange the code a bit to have a better understanding of this situation: let’s divide this line in two parts.

Explanations on a partly covered line

Explanations on a partly covered line

In fact, we go through the first part of the AND but not in the second one (Why ? Because the first part has returned false).


A line of code can get the following color code:

  • Green: Line is fully covered, a test (at least) execute this line;
  • Red: Line has not been executed at all, you have to write a new test;
  • Yellow: Line is partly covered, it contains covered code and not-covered code. You can arrange code, like I did, to know which code you need to test.


EclEmma provides two different coverage rates, the one on the business code (this is the most important rate, the one which needs to be monitored and be improved), and also a rate on tests itselves. This one is also useful, because showing you the lines of code not executed in your tests. In other words, it helps you to refactor your tests.


Lastly, you can check out for other measures. For each Java element (Java project, source folder, package, type or method) EclEmma provides a Coverage property page summarizing all coverage counters. You can access to this page on the project properties. These figures can be useful for the developer, the project manager, or be part of a customer report.

Coverage properties

Coverage properties


I think EclEmma default configuration is good and enough. Nevertheless, you can modify some options such as the color highlighting or add the rate on each file in the package explorer view (see below).

Adding the coverage decorator

Adding the coverage decorator


Conclusion

According to me, EclEmma (Emma generally speaking) is an essential software every Java test-unit developers should have in their “toolbox”.

It will help you to reach a higher coverage rate (and thus, improve your software quality). Maybe one day, you will succeed in reaching a 100% coverage rate (but… Nothing is less certain, because the code is not as fully testable as we want it to be… this is another story… and especially a different topic).

Related posts

Java 11A Java 11 migration successful story devoxxDevoxx UK 2018 – Day 2 Java User GroupJava User Group – JUG microservices-legoMicroservices architecture – Best practices
Comments
2 Comments »
Categories
Agile programming
Tags
code coverage, eclemma, eclipse, emma, java, plugin, unit test
Comments rss Comments rss
Page 1 of 11
Download CarmaBlog App

RSS feeds

  • RSS feed RSS - Posts
  • RSS feed RSS - Comments

Most viewed posts

  • Changing the language in Firefox - 115,579 views
  • Using Google Forms / Drive / Docs to create an online survey - 63,166 views
  • FAQ – Online survey with Google Forms / Drive / Docs - 52,403 views
  • Customizing Gnome 3 (Shell) - 30,017 views
  • The meaning of URL, URI, URN - 17,251 views
  • Java EE & CDI vs. Spring - 15,442 views
  • Open Street Map, better map than Google Maps? - 14,648 views
  • Comparing NoSQL: Couchbase & MongoDB - 14,082 views
  • Firefox Nightly, Aurora, Beta, Desktop, Mobile, ESR & Co. - 13,087 views
  • API, REST, JSON, XML, HTTP, URI… What language do you speak? - 12,718 views

Recent Comments

  • Pauline on FAQ – Online survey with Google Forms / Drive / DocsMerci Fabian, mais le but étant que nos clients pu…
  • Fabian Piau on FAQ – Online survey with Google Forms / Drive / DocsProbablement mais ces options sont en général paya…
  • Pauline on FAQ – Online survey with Google Forms / Drive / DocsBonjour Fabian, Merci de votre retour, oui j'avais…
  • Fabian Piau on FAQ – Online survey with Google Forms / Drive / DocsBonjour Pauline, ce n'est pas possible de créer un…
  • Pauline on FAQ – Online survey with Google Forms / Drive / DocsBonjour, Je suis en train de créer un Google Forms…

Recent posts

  • How to write a blog post? At least my way! - 3 months and 2 weeks ago
  • Bot Attacks: You are not alone… - 1 year and 11 months ago
  • Flagger – Monitor your Canary deployments with Grafana - 2 years and 8 months ago
  • Flagger – Canary deployments on Kubernetes - 2 years and 10 months ago
  • Flagger – Get Started with Istio and Kubernetes - 2 years and 10 months ago
  • Expedia CoderDojo in London - 3 years and 7 months ago
  • Volunteering at Devoxx4Kids - 3 years and 10 months ago
  • A Java 11 migration successful story - 4 years and 2 months ago
  • Tips to make your WordPress website secure - 4 years and 5 months ago
  • Devoxx UK 2018 – Day 2 - 4 years and 9 months ago
  • Devoxx UK 2018 – Day 1 - 4 years and 9 months ago
  • Wise, Revolut and Monzo, a small revolution for travelers and expats - 5 years and 1 month ago
  • Autocomplete for Git - 5 years and 10 months ago
  • Swagger, the automated API documentation - 6 years and 2 weeks ago
  • Microservices architecture – Best practices - 6 years and 5 months ago
Buy me a coffee

Language

  • Français
  • English

Follow me!

Follow me on Linkedin
Follow me on Twitter
Follow me on Stackoverflow
Follow me on Github
Follow me on Rss
Link to my Contact

Email subscription

Enter your email address to receive notifications of new posts.

Tags

.net agile agility android bash best practices blog cache cloud computing conference continuous integration css developer devoxx docker eclipse extreme programming firefox flagger google helm hibernate istio java job jug kubernetes london mobile computing overview performance plugin programmer script security sharing society spring tdd test tool ubuntu windows wordpress

Links

  • Blog Ippon Technologies
  • Blog Publicis Sapient
  • Blog Zenika
  • Classpert
  • CommitStrip
  • Coursera
  • Le Touilleur Express
  • Les Cast Codeurs Podcast
  • OCTO talks !
  • The Twelve-Factor App

Categories

  • Event (15)
  • Linux (3)
  • Management (8)
  • Agile programming (29)
  • Technology (45)

Archives

  • December 2022 (1)
  • April 2021 (1)
  • June 2020 (1)
  • May 2020 (2)
  • July 2019 (1)
  • May 2019 (1)
  • December 2018 (1)
  • October 2018 (1)
  • June 2018 (1)
  • May 2018 (1)
  • January 2018 (1)
  • May 2017 (1)
  • March 2017 (1)
  • October 2016 (1)
  • April 2016 (2)
  • March 2016 (1)
  • November 2015 (1)
  • May 2015 (1)
  • February 2015 (1)
  • December 2014 (1)
  • November 2014 (1)
  • September 2014 (2)
  • August 2014 (1)
  • July 2014 (2)
  • June 2014 (1)
  • April 2014 (1)
  • March 2014 (1)
  • February 2014 (2)
  • January 2014 (1)
  • December 2013 (1)
  • November 2013 (1)
  • October 2013 (3)
  • September 2013 (5)
  • July 2013 (1)
  • June 2013 (1)
  • May 2013 (1)
  • April 2013 (1)
  • March 2013 (2)
  • February 2013 (1)
  • January 2013 (2)
  • December 2012 (2)
  • October 2012 (1)
  • September 2012 (1)
  • July 2012 (1)
  • May 2012 (1)
  • April 2012 (1)
  • March 2012 (1)
  • February 2012 (1)
  • January 2012 (2)
  • December 2011 (1)
  • November 2011 (2)
  • October 2011 (2)
  • September 2011 (1)
  • July 2011 (1)
  • June 2011 (2)
  • April 2011 (1)
  • March 2011 (1)
  • February 2011 (1)
  • January 2011 (2)
  • November 2010 (2)
  • September 2010 (1)
  • August 2010 (1)
  • July 2010 (1)
  • June 2010 (1)
  • May 2010 (1)
  • April 2010 (1)
  • March 2010 (1)
  • February 2010 (1)
  • December 2009 (1)
  • November 2009 (1)
  • October 2009 (2)
  • September 2009 (2)
  • August 2009 (3)
  • July 2009 (1)
  • June 2009 (2)
Follow me on Twitter
Follow me on Linkedin
Follow me on Stackoverflow
Follow me on Rss
Link to my Contact
Follow me on Github
 
Fabian Piau | © 2009 - 2023
All Rights Reserved | Top ↑