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

Mar*_*ix1 3 java junit java.util.scanner

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

我将如何为此创建一个 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);
                   }
           }
       }
Run Code Online (Sandbox Code Playgroud)

我的数据文件看起来像这样

||
*Jack_Davis
*Sophia_Harrolds
Run Code Online (Sandbox Code Playgroud)

我曾尝试在我的测试方法中使用此代码

@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);                
}
Run Code Online (Sandbox Code Playgroud)

gui*_*ina 5

试试这个,例如:

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

@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
}
Run Code Online (Sandbox Code Playgroud)

如果要自动化控制台输入,请使用:

感谢:JUnit:如何模拟 System.in 测试?

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);
Run Code Online (Sandbox Code Playgroud)

小心catch里面的异常,以一个“好”的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
}
Run Code Online (Sandbox Code Playgroud)

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

assertEquals(myobject.scaninput_and_compare(filename,"Jack"), "Davis");
Run Code Online (Sandbox Code Playgroud)