dimanche 3 janvier 2016

Unit testing a console input method Java

How is possible to unit test the following method?

public static String grabString() {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
do {
  try {
    // call the readline method of the buffered reader object.
    return r.readLine();
  } catch (Exception e) {
  }
} while (true);}

If I call this method from the test class like so

String getInput = grabString();

And test it with

assertEquals(getInput, "hello");

Is it possible to use code to enter "hello" into the console without the need of typing so that the unit test can run instantly?

Aucun commentaire:

Enregistrer un commentaire