有没有办法知道FileReader指向的文件?

时间:2016-03-15 16:54:17

标签: java junit testng filereader filewriter

我试图用TestNG框架测试这段Java代码:

  var threads = GmailApp.search('label:"inbox" older_than:1d');
  for (j = 0; j < threads.length; j+=batchSize) {
    /* If (getContactsByEmailAddress('address from getFrom()') = null)
    GmailApp.moveThreadsToArchive(threads.slice(j, j+batchSize)) */
  }

我的测试类如下(对createFileIfNotExists(String path)和getFile(String path)的测试都已通过):

public static void createFileIfNotExists(String path) throws IOException{
    File file = new File(path);
    if(!file.exists()){
        file.createNewFile();
    }
}

public static File getFile(String path) throws IOException{
    createFileIfNotExists(path);
    return new File (path);
}

public static FileWriter initiateFileWriter (String path) throws IOException{
    return new FileWriter (getFile(path),false);
}


public static FileReader initiateFileReader (String path) throws IOException{
    return new FileReader (getFile(path));
}

但是,由于以下原因,对initiateFileWriter和initiateFileReader的测试都失败了:

initiateFileReader失败:

  

java.lang.AssertionError:expected:java.io.FileReader@53a07924但是:java.io.FileReader@4c715560

initiateFileWriter失败:

  

java.lang.AssertionError:expected:java.io.FileWriter@4960e09f但是:java.io.FileWriter@773b0c5b

我认为这是因为预期的和实际的File expectedFile, actualFile; String path; @BeforeMethod public void setUp() { path = "fixtures\\prueba.txt"; expectedFile = new File(path); } @BeforeGroups(groups = "IOReader") public void setUpIOReader() throws IOException { FileBody.createFileIfNotExists(path); } public void testCreateFileIfNotExists() throws IOException { FileBody.createFileIfNotExists(path); assertTrue(expectedFile.exists()); } public void testGetFile() throws IOException { actualFile = FileBody.getFile(path); assertEquals(expectedFile, actualFile); } public void testInitiateFileWriter() throws IOException { FileWriter expectedFileWriter = new FileWriter(expectedFile,false); FileWriter actualFileWriter = FileBody.initiateFileWriter(path); assertEquals(expectedFileWriter, actualFileWriter); expectedFileWriter.close(); actualFileWriter.close(); } @Test(groups = {"IOReader"}) public void testInitiateFileReader() throws IOException { FileReader expectedFileReader = new FileReader(expectedFile); FileReader actualFileReader = FileBody.initiateFileReader(path); assertEquals(expectedFileReader, actualFileReader); expectedFileReader.close(); actualFileReader.close(); } / FileWriter是不同的对象,但是......如果两个对象是等价的,那么FileReader方法不应该比较比指向同一个实例?

是否存在从assertEquals / FileWriter调用并知道他们指向哪个文件的方法?因为这样我可以解决这个问题:

FileReader

1 个答案:

答案 0 :(得分:3)

FileReader / FileWriter上没有方法可以告诉您它指向的文件(因为理论上您永远不需要知道 - 这就是您与之合作的原因)读者而不是File)。

您可以使用反射来阅读基础FileInputStream / FileOutputStream的私有字段。

但我认为正确的方法是 不要测试无法破解的东西 - 即,不要测试Java核心库。除非发布的代码是简化版,否则testInitiateFileWriter()正在有效地测试new FileWriter (getFile(path),false),并且您已经知道getFile(path)有效(根据您的通过测试),所以您唯一可以做到这一点。这里重新测试的是new FileWriter(...)

脚注:当然,Java库可以打破/包含错误,但在大多数情况下,对于像FileReader / FileWriter这样广泛使用的部分,机会可以忽略不计。