gpt4 book ai didi

java - 如何使用 JUnit 测试扫描仪的方法?

转载 作者:行者123 更新时间:2023-11-29 07:37:15 25 4
gpt4 key购买 nike

我有一个类可以读取文件并使用扫描仪接收用户输入,如果扫描仪等于该文件中一行的一部分,它将显示来自同一行的字符串。

我将如何为此创建一个 Junit 测试方法?

这是我的一些代码,我想要一个测试方法:

Scanner Input = new Scanner(System.in);
String name = Input.nextLine();

BufferedReader br;
try{
br = new BufferedReader(new FileReader(new File(filename)));
String nextLine;
while ((nextLine = br.readLine()) != null)
{
if (nextLine.startsWith("||"))
{
int f1 = nextLine.indexOf("*");
int f2 = nextLine.indexOf("_");
fName = nextLine.substring(f1+1, f2);
if (name.equals(fname))
{
String[] s1 = nextLine.split("_");
String sName = s1[1];
System.out.println(sName);
}
}
}

我的数据文件是这样的

||
*Jack_Davis
*Sophia_Harrolds

我已经尝试在我的测试方法中使用这段代码

@Test
public void testgetSurname() {
System.out.println("get surname");
String filename = "";
String expResult = "";
String result = fileReader.getSurname(filename);
assertEquals(expResult, result);

filename = "datafiles/names.txt";
String data = "Jack";
InputStream stdin = System.in;
try{
System.setIn(new ByteArrayInputStream(data.getBytes()));
Scanner scanner = new Scanner(System.in);
System.out.println(scanner.nextLine());
} finally {
System.setIn(stdin);
expResult = "Davis";
}
String result = fileReader.getSurname(filename);
assertEquals(expResult, result);
}

最佳答案

例如试试这个:

您可以通过自动模拟控制台来增强它(见下文)

@Test
public void test_scan() throws Exception
{
Myclass myobject=new myobject(); // with args

myobject.load(filename); // you must definie the filename

String result=myobject.scaninput_and_compare(); // you must use scan in, and compare

if (!result.equals(what_I_am_expecting) throw new Exception("EXCEPTION scaninput_and_compare");

// If you arrive here, it's OK
}

如果你想自动化控制台输入,使用:

礼貌:JUnit: How to simulate System.in testing?

String data = "What_I_could_put_in_console";
InputStream stdin = System.in;
System.setIn(new ByteArrayInputStream(data.getBytes()));
Scanner scanner = new Scanner(System.in);
System.setIn(stdin);

小心catch里面的Exception,以一个“好”的System.in结束。单独测试是可以的,几个的话,你应该验证一下。

使用您的代码:

public String scaninput_and_compare(String filename)
{
Scanner Input = new Scanner(System.in);
String name = Input.nextLine();

BufferedReader br;
try{
br = new BufferedReader(new FileReader(new File(filename)));
String nextLine;
while ((nextLine = br.readLine()) != null)
{
if (nextLine.startsWith("||"))
{
int f1 = nextLine.indexOf("*");
int f2 = nextLine.indexOf("_");
fName = nextLine.substring(f1+1, f2);
if (name.equals(fname))
{
String[] s1 = nextLine.split("_");
String sName = s1[1];
return sName;
}
}
}
// NO GOOD
return "lose";
}


@Test
public void test_scan() throws Exception
{
Myclass myobject=new myobject(); // with args

String filename="good_filename";

// MOCK System.in
String data = "Jack";
InputStream stdin = System.in;
System.setIn(new ByteArrayInputStream(data.getBytes()));

String result=myobject.scaninput_and_compare(filename); // you must use scan in, and compare

// RESTABLISH System.in
Scanner scanner = new Scanner(System.in);
System.setIn(stdin);

if (!result.equals("Davis") throw new Exception("EXCEPTION scaninput_and_compare");

// If you arrive here, it's OK
}

更好的设计,更轻松的测试:将 System.in 的扫描器与文件解析分开。只需用 (filename, fname) 做一个函数,它会直接测试:

assertEquals(myobject.scaninput_and_compare(filename,"Jack"), "Davis");

关于java - 如何使用 JUnit 测试扫描仪的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34139658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com