How to get 100% code coverage in recursive method?

I have such method in my program

public int getX() {
    System.out.println("Input x:");
    int rez = 0;
    Scanner scan1 = new Scanner(System.in);

 // Checking that the entered value is an integer
    if (scan1.hasNextInt()) {
        int num = scan1.nextInt();

        if ((num >= 0) && (num <= 100)) // range of acceptable values
        {
            rez = num;
        } else {
            System.out.println("You made a mistake when entering a number. Try again.");
            rez = getX();// recursion
        }
    } else {
        System.out.println("You must enter an integer. Try again.");
        scan1.next(); // Clearing the input buffer
        rez = getX();// recursion
    }

    return rez;
}

There are my tests for getX()

@ParameterizedTest
@CsvSource({"50", "0", "100"})
void validInputWithinRange(int input) {
    System.setIn(new ByteArrayInputStream(String.valueOf(input).getBytes()));
    Salary obj = new Salary();
    assertEquals(input, obj.getX());
    System.setIn(System.in); // Reset System.in
}

@ParameterizedTest
@CsvSource({"-1", "101", "abc","34.5"})
void invalidInputOutsideRangeOrNonInteger(String input) {
    System.setIn(new ByteArrayInputStream(input.getBytes()));
    Salary obj = new Salary();
    assertThrows(NoSuchElementException.class, obj::getX);
    System.setIn(System.in); // Reset System.in
}

I have 83.3% coverage, but how can I get 100%?

How do I capture these recursive calls?

enter image description here

I will be very grateful for your help, since I have not written tests before.

upd: here is full code of programm (it’s so simple, so i can’t get what’s the problem..)

public class Salary {
    static int x, y;
    static Scanner scan = new Scanner(System.in);

    
    public double test(int a, int b) {
        double result = 0;
        result = Math.sqrt(a * b);

        return result;
    }

    public int getX() {
    code above
    }

    public int getY() {
    same code as for getX()
    }

    public void printRez(int x, int y) {
        System.out.println("Result: " + test(x, y));
    }

    public static void main(String[] args) {

        Salary app = new Salary();
        x = app.getX();
        y = app.getY();
        app.printRez(x, y);
    }
}

  • Try adding one more assert in your second test assertThrows(InputMismatchException.class, obj::getX); and check

    – 

  • @mandy8055 still the same 🙁

    – 

  • Something is off. Either there is an exception keeping that line frim getting hit, or the coverage report was generated against a different version of the code – my best guesses.

    – 




  • @ash i added full code to my question. It is very simple, so I do not know what here can prevent getting into these lines. Maybe writing the test for main() will solve this problem if I simulate incorrect input of values, but I haven’t figured out how to write it yet…

    – 

  • Don’t use recursion just for looping. Use it when you have context to stack.

    – 

Leave a Comment