我有這樣一個測試:爲什麼在我的字符串值後有括號?
String dir = "/foo";
String fileName = "hello.txt";
String pathString = dir + "/" + fileName;
String text = "hello world!";
MyTask task = new MyTask();
FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
Path foo = fs.getPath(dir);
Files.createDirectory(foo);
Path hello = foo.resolve(fileName);
Files.write(hello, ImmutableList.of(text), StandardCharsets.UTF_8);
task.setFileSystem(fs);
String fileValue = task.readFile(pathString);
// Doing this just shows "fail whale: hello world!"
// fail("fail whale: " + fileValue);
// Failure from here adds brackets
assertEquals(text, fileValue);
我得到一個失敗這樣的:
org.junit.ComparisonFailure: expected:<hello world![]> but was:<hello world![
]>
如果我扭轉論據,我可以看到readFile
絕對是創造新的生產線,它的不只是我的終端截斷。
我不知道爲什麼JUnit在我的字符串之後顯示括號,而且我更加困惑的是爲什麼會添加一個空格。
readFile
很簡單:
public String readFile(String path) {
try {
return new String(Files.readAllBytes(fs.getPath(path)));
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
Java: Is assertEquals(String, String) reliable?使得String
的參考是一個Object
,但如果是這樣的話,一個括號只是意味着Object
,還是真搞不清楚新線。
'Files.write'在行末寫行結尾(\ n)符號 –