Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

The count of branches seems incorrect #134

Closed
xflotus opened this issue Feb 5, 2017 · 1 comment
Closed

The count of branches seems incorrect #134

xflotus opened this issue Feb 5, 2017 · 1 comment
Labels

Comments

@xflotus
Copy link

xflotus commented Feb 5, 2017

Steps to reproduce

the class to be tested:

import java.lang.Exception;

public class InsuredPerson {
	int age;
	char gender;
	boolean married;

	public InsuredPerson(int age, char gender, boolean married) {
		this.age = age;
		this.gender = gender;
		this.married = married;
	}

	public int premium() {
		int premium;
		if (age<25 && gender=='M' && !married)
			premium = 100;
		else
			premium = 0;
		return premium;
	}
}

the class to test:

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class PremiumTest {
	InsuredPerson isu;

	@Test
	public void test01() throws Exception {
		isu = new InsuredPerson(30, 'F', true);
		int p = isu.premium();
		assertEquals(0, p);
	}
	
	@Test
	public void test02() throws Exception {
		isu = new InsuredPerson(30, 'F', false);
		int p = isu.premium();
		assertEquals(0, p);
	}
	
	@Test
	public void test03() throws Exception {
		isu = new InsuredPerson(30, 'M', true);
		int p = isu.premium();
		assertEquals(0, p);
	}
	
	@Test
	public void test04() throws Exception {
		isu = new InsuredPerson(30, 'M', false);
		int p = isu.premium();
		assertEquals(0, p);
	}
	
	@Test
	public void test05() throws Exception {
		isu = new InsuredPerson(20, 'F', true);
		int p = isu.premium();
		assertEquals(0, p);
	}
	
	@Test
	public void test06() throws Exception {
		isu = new InsuredPerson(20, 'F', false);
		int p = isu.premium();
		assertEquals(0, p);
	}
	
	@Test
	public void test07() throws Exception {
		isu = new InsuredPerson(20, 'M', true);
		int p = isu.premium();
		assertEquals(0, p);
	}
	
	@Test
	public void test08() throws Exception {
		isu = new InsuredPerson(20, 'M', false);
		int p = isu.premium();
		assertEquals(100, p);
	}
}

After coverage running, point to the compound condition

       if (age<25 && gender=='M' && !married)

there will be a message: "All 6 branches covered." IMO, the count of branches should be 8 rather than 6.

EclEmma version: 2.3.3
Eclipse version: Neon
Operating system: Windows 7

Expected behaviour

8

Actual behaviour

6

@Godin
Copy link
Member

Godin commented Feb 5, 2017

EclEmma based on JaCoCo. JaCoCo analyzes Java bytecode, this is stated on page describing counters - https://www.eclemma.org/jacoco/trunk/doc/counters.html
In bytecode all constructions such as

if (a && b && c) something();

are decomposed into something like

if (a) if (b) if (c) something();

There are 6 edges in such control-flow-graph and 6 ways to go thru it, and 6 is exactly the value, which JaCoCo reports as branch coverage.

@Godin Godin closed this as completed Feb 5, 2017
@Godin Godin added the invalid label Feb 5, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants