2016-10-31 32 views
1

我是新手。我正在进行JUnit测试。以下是我测试的内容:我有一个包含记录列表的输入文件。如果记录有电子邮件,我必须将其添加到我的输出文件中,名为myemailfile.txt检查某个记录是否在文件中

在我的JUnit测试中,我必须测试带有[email protected]的人是否包含在myemailfile.txt中

有人可以请告知如何通过该文件,并检查这样的记录是否使它到文件。

Here is my file: 

    First Name,Last Name,Email,Id 
    John  ,Dough  ,[email protected]      ,12345 
    Jane  ,Smith  ,[email protected]      ,86547 
    Mary  ,Wells  ,[email protected]      ,76543 

以下是我的测试

@Test 
public void isRecordIncludedInEmailFile() throws IOException{  

    String testFile = "C:/Users/myname/myemailfile.txt"; 
    BufferedReader br = null; 
    String line = ""; 
    String fileSplitBy = ","; 

    try { 

     br = new BufferedReader(new FileReader(testFile)); 
     while ((line = br.readLine()) != null) { 

      // use comma as separator 
      String[] field = line.split(fileSplitBy); 

    //read through the file and see if the email that I expect ([email protected]) exists in the file 
    System.out.println("Email [email= " + field[2] + " , first name=" + field[0] + "]"); 

    //the line below should assert if "[email protected]" exists in the file    
    // assertEquals("[email protected]", field[2]); 
} 

谢谢

回答

1

你似乎错过的单元测试的关键点:你用它们来测试 Java类;不是某个文件包含某些内容。

换句话说,合理的事情在这里:

  1. 创建表示这样的记录,也可以叫做PersonInformation
  2. 编写代码读取这样的文件,并转将文件内容插入某个数组或者列表中PersonInformation objects
  3. 然后你编写一个单元测试,用一些数据创建一个假文件;您运行您的代码...并测试是否找到了预期的对象,并将其读入并存储在该列表中。

和最后提示:除非这是一个“学习锻炼”,你要手动解析文件内容。您会看到,该数据使用CSV格式(逗号分隔值)。编写代码读取这些数据并解析它意味着重新发明车轮。那里有很多图书馆可以为你工作。

相关问题