Mac 上的 TemporaryFolder 和 File#getCanonicalFile()

TemporaryFolder and File#getCanonicalFile() on Mac

以下 JUnit 4 测试在 Linux 和 Windows 下工作正常:

public class TmpFileTest {

    @Rule
    public TemporaryFolder tmp = new TemporaryFolder();

    @Test
    public void test() throws Exception {
        File tmpFile = tmp.newFile();
        Assert.assertEquals(tmpFile, tmpFile.getCanonicalFile());
    }

}

但断言在 Mac 上失败(使用 Sierra 10.12.4 测试):

java.lang.AssertionError: 
Expected :/var/folders/q4/rj3wqzms2fdcqlxgdzb3l5hc0000gn/T/junit1451860092188597816/junit1906079638039608483.tmp
Actual   :/private/var/folders/q4/rj3wqzms2fdcqlxgdzb3l5hc0000gn/T/junit1451860092188597816/junit1906079638039608483.tmp

var是指向private/var的符号链接,通过File#getCanonicalFile()解析——这就是区别。

有办法解决这个问题吗?这会在我的机器上导致一堆测试失败。

我想你还需要验证两条路径是否相同。

因此,与其尝试解决 getCanonicalFile() 的工作方式(它解析符号链接,没有办法),您可以接受它,并且在最终测试中您可以使用 getCanonicalFile()两端。

File expectedFile = ...
File actualFile = ...
assertEquals(expectedFile.getCanonicalFile(), tmpFile.getCanonicalFile());

那个,或者:

java.nio.file.Files.isSameFile(expectedFile.toPath(), actualFile.toPath())

正如 所指出的,最好的办法是简单地修复测试而不是解决给定的行为。

但是,快速破解可以使测试通过。通过使用替代 TemporaryFolder 构造函数(自 JUnit 4.11 起可用;请参阅 this answer 了解以前的版本),可以设置父文件夹:

@Rule
public TemporaryFolder tmp = new TemporaryFolder(System.getProperty("os.name").startsWith("Mac")
        ? new File("/private/" + System.getProperty("java.io.tmpdir"))
        : new File(System.getProperty("java.io.tmpdir")));

使用 Apache Commons Lang 中的 SystemUtils#IS_OS_MAC 使它更具可读性。