Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4117050
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:47:12+00:00 2026-05-20T22:47:12+00:00

This has been asked before, but was not clarified to the point where I

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T22:47:12+00:00Added an answer on May 20, 2026 at 10:47 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Now this question has been asked before but I am not able to get
This has been driving me nuts. I hope it's not been asked before but
I'm not sure if this question has been asked before but are there any
I am sure this has been asked before but my google-fu was not able
I know this question has been asked before but the solutions did not work
You would think this one has been asked before but I cant find it.
This question has been asked before, but not answered. Is there a list anywhere
I'm not sure if this question has been asked before but what are the
So this question has been asked before , but not answered in great detail.
I'm sure this has been asked before but I'm not even really sure what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.