有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

为什么Java测试覆盖率在使用构建器模式时报告未覆盖的类

我向几个复杂的类添加了一个构建器模式,当我对测试进行覆盖时,所有使用构建器模式的类现在都报告测试中有一个未覆盖的类

下面是一个简化的例子:

public class CoverageWithBuilder {

    public int fInt;

    public static class Builder {
        public Integer fInteger = null;

        public Builder setInt(int i) {
            fInteger = i;
            return this;
        }

        public CoverageWithBuilder build() {
            if (fInteger == null)
                throw new IllegalStateException("Oops");

            return new CoverageWithBuilder(fInteger);
        }

    }

    private CoverageWithBuilder(int i) {
        fInt = i;
    }

    public int getInt() {
        return fInt;
    }
}

以下是测试:

import org.junit.Test;

import static org.junit.Assert.*;

public class CoverageWithBuilderTest {

    @Test
    public void test1() {
        CoverageWithBuilder t = new CoverageWithBuilder.Builder().setInt(9).build();
        assertEquals(9, t.getInt());
    }

    @Test(expected = IllegalStateException.class)
    public void test2() {
        new CoverageWithBuilder.Builder().build();
    }
}

覆盖率(将IntelliJ Idea与任何覆盖工具选项——IntelliJ、Emma、JaCoCo一起使用)仅报告部分课程覆盖率:

CoverageWithBuilder .... 66% (2/3) . 100% (6/6) . 100% (12/12)  

覆盖工具所说的第三类是什么


共 (0) 个答案