0

I'm testing some methods with JUnit4 and when I run the test class my program stops at the line where the scanner requires user input. My goal is to have it not stop and conduct all the tests without having to input anything in the console.

I've tried messing around with input and output streams, but they either happen before or after I run the test, not during.

Here is the code that I'm trying to test:

String userName = theUsername;
String password = thePassword; 
Scanner myInput = new Scanner();
/**
 *the while loop tests to see if the given password is the password
 *that is assosiated with that username
 */
while (!myUserList.get(userName).getMyPassword().contentEquals(password)) {
    System.out.println("Wrong Credentials");
    System.out.print("User Name:");
    userName = myInput.next();
    System.out.print("Password:");
    password = myInput.next();
}
System.out.println("Login Successfull");
return true;

And here is the test that I'm tring to run:

public void testLoginIncorectPass() {
    assertNotNull("testLogin return", reg.login(oldUser.getMyName(), "2"));
}

When I run it, the test conducts properly but it gets stuck at

username = myInput.next();

and won't finish until I input something.

nkehm
  • 1
  • 1
  • 1
    This code wouldn't compile. Scanner doesn't have a no-arg constructor. What are you *really* trying to test? How is the method `reg.login()`defined? – JB Nizet Oct 26 '19 at 20:24
  • It's not that easy to help you when you removed the information from where Scanner is reading its input. But I assume it is `System.in`, so this question has been asked before. – Tom Oct 26 '19 at 20:24
  • 2
    Possible duplicate of [JUnit testing with simulated user input](https://stackoverflow.com/questions/6415728/junit-testing-with-simulated-user-input) – Tom Oct 26 '19 at 20:25

0 Answers0