Details

Recommendation type: Uncovered code

Configurable in Preferences: Yes

Filter category: Uncovered code

Description

This type of recommendation helps you identify code that is not covered.

  • Click the Go to link to navigate to the uncovered code associated with the recommendation.
  • Click the Highlight link to highlight the issue in the execution flow displayed in the Unit Test Assistant view.
  • Click the Clone test action link to display a list of already existing tests that can help you to quickly cover the detected code. Click OK to make a copy of the selected test. UTA preselects the most suitable test case by analyzing the test code.

See Increasing Code Coverage for details.

Example

In the following example, the UncoveredCode class contains two methods: the normalize method that contains a conditional statement, and the readChar method that catches an exception:

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);
        }
    }
}

The following test cases have been created to cover the methods:

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);
    }
}

When the test cases are run, UTA detects that they do not fully cover the code of the normalize and readChar methods:

As a result, UTA displays an Uncovered code recommendation for each of the methods:

Clicking the Clone test links automatically creates a duplicate of the selected test case to help you cover the uncovered lines  (see Cloning and Updating a Test Case for details).


  • No labels