【问题标题】:Files.write and Files.readAllBytes changes byte[] input [duplicate]Files.write 和 Files.readAllBytes 更改 byte[] 输入 [重复]
【发布时间】:2018-12-08 21:40:58
【问题描述】:

我正在编写一个单元测试,但我看到以下我不明白的内容。任何帮助表示赞赏。

tempFile = File.createTempFile("temp", "file");
Files.write(tempFile.toPath(), expectedBytes);
byte[] b = Files.readAllBytes(tempFile.toPath());
Assert.equals(b, expectedBytes); // failed

【问题讨论】:

  • 一个字节数组只等于它自己。不是任何其他字节数组,即使其他数组具有相同的字节。顺便说一句,所有数组也是如此。
  • 尝试 Assert.assertArrayEquals() 代替(假设这里是 JUnit)。

标签: java


【解决方案1】:

数组不支持与equals 进行深度比较。检查 Assert 类是否有 arrayEquals 方法。

【讨论】:

  • 行得通,非常感谢!
猜你喜欢
  • 2012-12-14
  • 2021-07-04
  • 1970-01-01
  • 2023-04-05
  • 2021-11-05
  • 2016-01-08
  • 2018-02-25
  • 2018-04-17
  • 1970-01-01
相关资源
最近更新 更多