Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用JUnit4跳过扫描仪输入_Java_Java.util.scanner_Junit4 - Fatal编程技术网

Java 如何使用JUnit4跳过扫描仪输入

Java 如何使用JUnit4跳过扫描仪输入,java,java.util.scanner,junit4,Java,Java.util.scanner,Junit4,我正在用JUnit4测试一些方法,当我运行测试类时,我的程序会在扫描程序需要用户输入的地方停止。我的目标是让它不停止并执行所有测试,而不必在控制台中输入任何内容 我尝试过处理输入和输出流,但它们要么发生在我运行测试之前,要么发生在运行测试之后,而不是在运行测试期间 下面是我要测试的代码: String userName = theUsername; String password = thePassword; Scanner myInput = new Scanner(); /** *the

我正在用JUnit4测试一些方法,当我运行测试类时,我的程序会在扫描程序需要用户输入的地方停止。我的目标是让它不停止并执行所有测试,而不必在控制台中输入任何内容

我尝试过处理输入和输出流,但它们要么发生在我运行测试之前,要么发生在运行测试之后,而不是在运行测试期间

下面是我要测试的代码:

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;
下面是我尝试运行的测试:

public void testLoginIncorectPass() {
    assertNotNull("testLogin return", reg.login(oldUser.getMyName(), "2"));
}
当我运行它时,测试运行正常,但它在

username = myInput.next();

在我输入一些东西之前不会完成。

此代码无法编译。扫描程序没有无参数构造函数。你到底想测试什么?方法
reg.login()
是如何定义的?当您从扫描仪读取其输入的位置删除信息时,帮助您并不容易。但我假设它是
系统。在
中,所以这个问题以前被问过。可能是重复的