0

I am running unit tests using maven. At one point I need to pause execution in order for the tester to do something. Once they have done it, they need to press a key. I have the following code in my unit test.

try {
    while (true) {

        System.out.print("Enter something : ");
        inputResult = System.console().readLine();

    }               
} catch (Exception exc) {
    // exception handling code to 
    // print out message and stack trace        
}

When this code is run, an exception is caught which has a stack trace leading to the line:

inputResult = System.console().readLine(); 

Why would this be happening? And how might I solve it?

radiobrain77
  • 613
  • 1
  • 6
  • 19
  • http://stackoverflow.com/questions/4644415/java-how-to-get-input-from-system-console – Suraj Rao Jan 06 '17 at 07:23
  • *"I am running unit tests using maven. At one point I need to pause execution in order for the tester to do something."* this is **not** a *unittest* , exclude that test from automatic execution. – Timothy Truckle Jan 06 '17 at 07:23

1 Answers1

0

You mixed up UnitTest with Tests executed by the JUnit framework.

By definition a UnitTest is some additional code, that verifies the behavior of the production code automatically, which in particular means without user interaction.

You have three choices (in order of effort needed):

  1. exclude this test(s) from beeing executed during maven build. You can do so by configuring the maven-surefire-plugin

  2. automate that user interaction too.

  3. improve your unittest.
    There is a chance that your test fails the characteristics of a UnitTest by not separating the tested unit.

    If you consequently apply the separation of concerns principle the user interaction is done in a different unit (class/module) which you should be replaced by a mock during runtime of unittests. These mocks are best created with support of a mocking framework like Mockito, JMock or alike.

I for myself would suggest #3.

Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51