Total Pageviews

2017/10/04

How to Improve Code Coverage in Enumeration Type ?

Problem
Assume I have a enumeration as bellows:
package albert.practice.designPattern.factory;

public enum ShapeEnum {
    CIRCLE, RECTANGLE, SQUARE, TRIANGLE, OVAL;
}


After I run code coverage test, the coverage rate in enumeration is low:


How to improve its code coverage rate?




How-To
To get rid of this issue, you need to call the values() methods in this enumeration. You can check the testShapeEnum() in the test code:
package albert.practice.designPattern.factory;

import static org.junit.Assert.assertEquals;

import java.util.function.Supplier;

import org.junit.Test;

public class ShapeFactoryTest {

    @Test
    public void testDifferentShape() {
        Supplier<ShapeFactory> shapeFactory = ShapeFactory::new;
        shapeFactory.get().getShape(ShapeEnum.CIRCLE).draw();
        shapeFactory.get().getShape(ShapeEnum.RECTANGLE).draw();
        shapeFactory.get().getShape(ShapeEnum.OVAL).draw();
        shapeFactory.get().getShape(ShapeEnum.SQUARE).draw();
        shapeFactory.get().getShape(ShapeEnum.TRIANGLE).draw();
    }

    @Test
    // https://stackoverflow.com/questions/4512358/emma-coverage-on-enum-types/4548912#4548912
    public void testShapeEnum() {
        ShapeEnum[] shapeEnums = ShapeEnum.values();
        assertEquals(5, shapeEnums.length);
    }

}







No comments: