Skip to content

How to test a method, that uses a Scanner? #448

Answered by bartfastiel
bartfastiel asked this question in Java
Discussion options

You must be logged in to vote

Only test the logic, not the scanning of input.

If you refactor your code like this:

boolean blubb() {
    Scanner s = new Scanner(System.in);
    return blubb(s.nextLine());
}

boolean bla(String line) {
    return line.contains("a");
}

Then you can test method bla with a usual unit-test.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by bartfastiel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Java
1 participant