详细信息

建议类型:未覆盖的代码

是否可在首选项配置:是

过滤器分类:未覆盖的代码

说明

该类型的建议可帮助您识别未覆盖的代码。

详细信息,请参阅增加代码覆盖率

示例

在下面的示例中,UncoveredCode 类包含两个方法:包含条件语句的 normalize 方法,捕获异常的 readChar 方法:

public class UncoveredCode {
    public int normalize(int value) {
        if (value > 100) {
            return 100;
        } else {
            return value;
        }
    }

    public char readChar(Reader reader) throws Exception {
        try {
            return (char)reader.read();
        } catch (IOException e) {
            throw new Exception("Encountered IO exception", e);
        }
    }
}

已创建以下测试用例来覆盖这些方法:

public class UncoveredCodeTest {
    @Test
    public void testNormalize() throws Throwable {
        UncoveredCode underTest = new UncoveredCode();

        int value = 0; // UTA: default value
        int result = underTest.normalize(value);

        assertEquals(0, result);
    }

    @Test
    public void testReadChar() throws Throwable {
        UncoveredCode underTest = new UncoveredCode();

        Reader reader = mock(Reader.class);
        int readResult = 'c';
        when(reader.read()).thenReturn(readResult);

        char result = underTest.readChar(reader);

        assertEquals('c', result);
    }
}

运行测试用例时,UTA 检测到未能完全覆盖 normalizereadChar 方法:

因此,UTA 会针对每个方法显示一个未覆盖的代码建议:

点击克隆试验链接会自动创建所选测试用例的副本,帮助您覆盖未覆盖的代码行(详细信息,请参阅克隆和更新测试用例)。