我正在尝试为下面给出的函数编写Junit测试用例:
class A{
int i;
void set()
{
Scanner in=new Scanner(System.in);
i=in.nextInt();
}
}
现在我的问题是,当我为它创建一个Junit测试用例时,它不接受用户的输入:
public void testSet() throws FileNotFoundException {
System.out.println("set");
A instance = new A();
int i=1;
instance.set(i);
// TODO review the generated test code and remove the default call to fail.
//fail("The test case is a prototype.");
}
请建议我如何接受用户的输入。
InputStream
进行测试,请将其附加到您的OutputStream
并通过编程方式提供输入。 - Victor SorokinSystem.in
读取,而是从传入的InputStream
或类似的对象中读取。 - Louis Wasserman