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

  • Home
  • SEARCH
  • 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 8765059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:11:36+00:00 2026-06-13T16:11:36+00:00

I have question about JUnit tests. I am trying to test my code and

  • 0

I have question about JUnit tests.

I am trying to test my code and for some unknown reason it keeps failing me all the time. This is code I try to test:

public void userInputWord() throws Exception{ 
    System.out.println("****Please input your word, 2 to 8 ***");
    System.out.println("**********character strings.**********");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    inputWord = br.readLine();
    setInputWord(inputWord);
    setWordLenght(inputWord.length());          
    System.out.println("***Please input amount of words you***");
    System.out.println("*************want to find*************");
    try{
        stringLadder = br.readLine();
        setLadder(Integer.parseInt(stringLadder));
    }catch(NumberFormatException nfe){
        System.out.println("Your input must be integer value.");
    }
    searchForMatch();
}

Basically this method is asking user to input two values (starting word and how many words he wants to see as a WordLadder) I am assigning those values to set methods. My JUnit for this class is following:

public void testUserInputWord() {
    Generation gener = new Generation();
    StringReader read = new StringReader("broom");
    Scanner scan = new Scanner(read);
    gener.setInputWord(scan.toString());
    assertEquals("broom", gener.getInputWord());
}

I never wrote JUnit before and just did some research, could anyone explain what am I doing wrong?

  • 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-06-13T16:11:38+00:00Added an answer on June 13, 2026 at 4:11 pm

    Well, the first problem is that you’re never calling the code you’re trying to test. All you’re testing at the moment is the getters and setters (for ladder and inputWord).

    Unfortunately, testing the userInputWord method is tricky, because it relies on System.in. You need to have a way of “faking” the user input. One option would be to make the method take a Scanner or perhaps a Reader. You could then create all the appropriate user input via a StringReader, so that the method would read the data you’d pre-programmed. You’d then test the results. If other parts of this class need user input too, you may want to change it so that your constructor takes something (again, possibly a Scanner, possibly a Reader) to represent user input. Again, you’d provide the test data when constructing your test instance.

    Fundamentally, the idea of “something to provide user input” is a dependency for the code you’re trying to test – and a lot of the skill of unit testing is working out how to provide dependencies which are under the control of your test. There are lots of approaches to this which are appropriate in different situations – in this case, you can probably just use an existing implementation (StringReader) as the underlying data source instead of the console.

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

Sidebar

Related Questions

I have a question about JUnit assertEquals to test double values. Reading the API
I have question about XSLT1.0. The task is to write out in HTML all
I have a question about how GC works in Java. Consider the following code:
I have a question about SqlDataReader: Is there some way to modificated the values
Edit: Not JUnit 4 available at this time. Hi there, I have a question
I have question about my code which does the Hoare Partition Method. Here is
I have a question about JUnit testing. Our JUnit suite is testing various functions
I have question about throw. How will the throw work in the following code?
I have question about the sql query. I am trying to make the query
I have question about regexp in TCL. How i can find and change some

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.