java 我如何在java中对用户输入进行单元测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16066671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:47:14  来源:igfitidea点击:

How can I unit test user input in java

javaunit-testing

提问by user1462617

I am trying to understand how can I test a user's input (please note I am not trying to do a mock test but a test of actual user's input)

我想了解如何测试用户的输入(请注意,我不是在尝试进行模拟测试,而是在测试实际用户的输入)

Currently as you may see in my program I have hard coded the values for my test case and it is passing all tests but how can I get a user's input and test it .

目前,正如您在我的程序中所看到的,我已经对我的测试用例的值进行了硬编码,并且它正在通过所有测试,但是我如何获取用户的输入并对其进行测试。

Is there a way where I can call System.in my constructor and pass it when I create an instance of MyClass1 in the test class?

有没有办法在我的构造函数中调用 System. 并在我在测试类中创建 MyClass1 的实例时传递它?

Please, if possible give me some example code so I can better understand.

如果可能,请给我一些示例代码,以便我更好地理解。

If I have a interface as such

如果我有这样的界面

public interface IMyClass{
   public int getvalue1();
   public int getvalue2();
   public int getvalue3();
}

and then interface implementation as such

然后接口实现

public class MyClass1 implements MyClass{

private int _value1 = 0;
private int _value2 = 0;
private int _value3 = 0;



public MyClass1(int number1, int number2, int number3)
{

   _value1 = number1;
    _value2 = number2;
    _value3 = number3;
}

public void setLength1(int value1)
{
    _value1 = value1;
}

public void setLength2(int length2)
{
    _value2 = value2;
}

public void setLength3(int length3)
{
    _value3 = value3;
}

public int getValue1()
{
    return _value1;
}

public int getValue2()
{
    return _value2;
}

public int getValue3()
{
    return _value3;
}
}

and finally a test class as such:

最后是一个测试类:

public class ClasTest extends TestCase {

public void testNumbers()
{
   MyClass1 numbers= new MyClass1(1,2,3);
   assertThat(numbers.getValue1(),is(not(numbers.getValue2())));

}
}

Thankyou, I appreciate any help.

谢谢,我很感激任何帮助。

回答by Mike Rylander

Use System.setIn(new InputSteam());and then write to the input stream passed in to System.in

使用System.setIn(new InputSteam());然后写入传入 System.in 的输入流

see: JUnit: How to simulate System.in testing?

请参阅:JUnit:如何模拟 System.in 测试?

Single Input

单输入

String data = "Users Input";
System.setIn(new ByteArrayInputStream(data.getBytes()));

Scanner scanner = new Scanner(System.in);
System.out.println(scanner.nextLine());

Result

结果

Users Input

Multiple Inputs

多个输入

String data = "Users Input" +
        "\nA second line of user input.";
System.setIn(new ByteArrayInputStream(data.getBytes()));

Scanner scanner = new Scanner(System.in);
System.out.println("Line 1: " + scanner.nextLine());
System.out.println("Line 2: " + scanner.nextLine());

Result

结果

Line 1: Users Input
Line 2: A second line of user input.

回答by djechlin

If on unix

如果在 unix

java MyProgram < sampleinput.txt

java MyProgram < sampleinput.txt

回答by NullPointerException

user Scanner class

用户扫描器类

public void testNumbers()
{

    Scanner scan = new Scanner(System.in);
    System.out.println("value1");
    int value1 = scan.nextInt();

    System.out.println("value2");
    int  value2 = scan.nextInt();

    System.out.println("value3");
    int value3 = scan.nextInt();

   MyClass1 numbers= new MyClass1(value1, value2, value3);
   assertThat(numbers.getValue1(),is(not(numbers.getValue2())));

}