1

This has been asked before, but was not clarified to the point where I get it. Similar to the one or two other threads I've seen on this subject, I'm working on a chat client with command line inputs for logging in/off, disconnecting, etc. and I am unsure how to simulate this in a JUnit test case. Other responses indicated that I should try changing the System.in to a separate InputStream but...then what?

tl;dr: I have a method in my actual code for parsing command line input, and need a JUnit way of testing that these were entered and appropriately processed.

1 Answer 1

1

EDIT: It seems I misunderstood the question. I usually use the term "command line input" to refer to command line arguments given to the process to start with, rather than interactive console input. However...

Handing your real code either a different InputStream or possibly even a Reader or Scanner would indeed help - anything to separate the "getting input" part from the console. You can then fake the input all in one go pretty easily, using a String as input in your test code, and then either converting it to bytes and wrapping those bytes in a ByteArrayInputStream or wrapping the string directly in StringReader.

The downside of this is that there's no easy way of making this "pause" after one command in order to check the results.

You may want to alter the design somewhat so that the part which reads the input is separated from the part which handles the input. The reading part could be a very simple loop, on the order of:

String line;
while ((line = reader.readLine()) != null) {
  handleInput(line);
}

You could then potentially leave that part untested by unit tests, or write some relatively primitive tests - but you can then test handleInput extensively, as it's now separated from the input source.


Original answer

If you've extracted the parsing code from the code which really starts the application, it's easy: run that code, and check the results. This will be easiest if you have some sort of class encapsulating the options, of course. For example, your main method might look like this:

public static void main(String[] args) {
  Options options = Options.parse(args);
  // Use options here
}

Then you can just test Options.parse very easily.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm probably just having an epic brainfart, but I don't think this really answers my question. I can start "Welcome, please enter commands:" loop inside my JUnit test case, but how do I then enter commands from aforementioned test case which has no main method?
@Nick: Ah, I see. By "command line input" I thought you mean command line arguments... whereas you really mean interactive console input. Will edit.
I think separating the handling from the reading is going to be my tack. Having a method that takes a string argument so I can manually pass it whatever I want in JUnit will make my life easy. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.