JUnit如何使用扫描仪测试方法? [英] How to JUnit test a Method with a Scanner?

查看:45
本文介绍了JUnit如何使用扫描仪测试方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a class which reads a file and takes in a user input with a scanner and if the scanner equals a part of a line in that file, it will display a string from the same line.

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

以下是我想要的一些测试方法的代码:

Here is some of my code that I want a test method for:

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);
                   }
           }
       }

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

my data file looks like this

||
*Jack_Davis
*Sophia_Harrolds

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

I have tried to use this code in my test method

@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);                
}

推荐答案

请尝试以下操作:

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

You could enhance it by simulate the console automatically (see below)

@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
}

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

If you want to automatize the console input, use that:

由以下人员提供: JUnit:如何在测试中模拟System.? a>

Courtesy of: 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异常,以完成一个好的"系统.单独进行测试是可以的,对于几个测试,您应该进行验证.

Beware of catch Exception inside, to finish with a "good" System.in It's ok for a test alone, for several, you should verify.

使用您的代码:

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)做一个函数,它将直接进行测试:

BETTER design, and testing more easy: separate your scanner of System.in, from your file parsing. Just do a function with (filename, fname), and it will be direct to test :

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

这篇关于JUnit如何使用扫描仪测试方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆