gpt4 book ai didi

java - 为什么我的字符串值后面有括号?

转载 作者:行者123 更新时间:2023-12-02 01:48:06 25 4
gpt4 key购买 nike

我有一个这样的测试:

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,仍然对这个新的感到困惑线。

最佳答案

JUnit assertEquals 方法将字符串中出现差异的部分放在方括号内,以帮助您突出显示问题。 (使用 assertEquals("abcde", "acedc") 或类似的方法来查看更直观的示例。)

在这种情况下,它表明一个字符串后面没有任何内容,而另一个字符串末尾有一个换行符。

Files.write documentation状态:

Each line is a char sequence and is written to the file in sequence with each line terminated by the platform's line separator, as defined by the system property line.separator.

关于java - 为什么我的字符串值后面有括号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46228041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com