Testing the coverage metric of JMockIt

After yesterdays article about Code Coverage terminology I thought more about the code coverage metric JMockIt uses. I created a simple class to get more insights:

CoverMeSimple.java

package de.kopis.katas;

public class CoverMeSimple {
    public int simple(int x, int y) {
        int z = x;

        if(y > x)
            z = y;
        z *= 2;

        return z;
    }
}

This is as simple as it gets. The example is taken from the Wikipedia page on Kontrollflussorientierte Testverfahren again. For 100% statement coverage you only need one test case, in which y > x. Then all statements are executed and you have 100% statement coverage. So I wrote this little unit test:

CoverMeSimpleTest.java

package de.kopis.katas;

import static org.junit.Assert.assertEquals;

import mockit.integration.junit4.JMockit;

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(JMockit.class)
public class CoverMeSimpleTest {
    @Test
    public void testSimple() {
        CoverMeSimple cm = new CoverMeSimple();
        int result = cm.simple(1, 2);
        assertEquals(4, result);
    }
}

And here is the resulting JMockit coverage report:

JMockit Coverage Report of CoverMeSimple

JMockit Coverage Report of CoverMeSimple

JMockit detailed Coverage Report on CoverMeSimple

JMockit detailed Coverage Report on CoverMeSimple

As you can see, JMockit tells us that every single statement is executed, exactly 1 time, with this test case. And that’s exactly what I understand as statement coverage and it is in full compliance to the ISTQB terminology.

Now, to make my point really clear, let me change the class as follows:

package de.kopis.katas;

public class CoverMeSimple {
	public int simple(int x, int y) {
		int z = x;

		if(y > x) {
			z = y;
		} else {

		}

		z *= 2;

		return z;
	}
}

I added the empty ELSE statement that I omitted first. And I run JMockit again to get a new coverage report:

JMockit Coverage report for modified CoverMeSimple

JMockit Coverage report for modified CoverMeSimple


JMockit detailed Coverage report for modified CoverMeSimple

JMockit detailed Coverage report for modified CoverMeSimple

This is not 100% branch coverage, ISTQB certified or not. ;-) My point on this is, if you want to use a tool to verify your testing requirements, make sure that you know what the tool is measuring.

And make sure you read the discussion thread in the JMockit users group.

*Update* I created a Cobertura coverage report for CoverMeSimple now:

CoverMeSimple Cobertura coverage report

CoverMeSimple Cobertura coverage report

Über Carsten

Ich bin leidenschaftlicher Softwareentwickler, studierter Dipl.Ing. Elektrotechnik, Gaming-verrückt, Kraftsportler und Einsiedler.
Dieser Beitrag wurde unter Softwareentwicklung abgelegt und mit , , , , , verschlagwortet. Setze ein Lesezeichen auf den Permalink.

4 Antworten auf Testing the coverage metric of JMockIt

  1. Carsten sagt:

    The report looks similar to Cobertura now, but I think that’s a result of measuring the same metrics. Very cool to see this feature in JMockIt! It enables the developer to check testing requirements while he is developing, not afterwards.

    Thanks for updating me with the link! :-)

  2. The path coverage metric in JMockit 0.994 is still a work in progress, but it IS true path coverage, not simple branch/decision coverage.

    Actually, it probably is the first ever such implementation for Java. (I googled around, but couldn’t find any “previous art”, beyond the TCAT tool for C++.)

  3. Carsten sagt:

    I’m looking forward to it. I’m really into testing and providing metrics to monitor the software development process. Jmockit already has a place in my personal toolset and maybe I can drop Cobertura again. I have to read on your path coverage metric, and hope that you provide some examples in the documentation.

    I think I’ll create a little example with a loop, an if and a switch to test the path coverage later today. :-)

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

*

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>