[英]Why are there brackets after my String's value?
我有這樣的測試:
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:assertEquals(String,String)可靠嗎? 使String
成為一個Object
引用,但如果是這種情況,括號只是暗示Object
,仍然真的對這個新行感到困惑。
JUnit assertEquals
方法在發生差異的字符串部分周圍放置方括號,以幫助突出顯示問題。 (使用assertEquals("abcde", "acedc")
或類似的東西來看一個更直觀的例子。)
在這種情況下,它表明一個字符串后面沒有任何內容,而另一個字符串在它的末尾有一個換行符。
每行都是一個char序列,並按順序寫入文件,每行由平台的行分隔符終止,由系統屬性
line.separator
定義。
括號用於突出字符串不同的位置。
在您的情況下,它會在實際結果的末尾突出顯示意外的換行符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.