简体   繁体   English

assertEquals在两个看似相同的字符串上失败了

[英]assertEquals failing on two seemingly identical Strings

Here is my test: 这是我的测试:

@Test
public void testAddPaperConfirm()
{
    String input = "P\n" +
                    "The Life of Geoff the Platypus";
    InputStream testInput = new ByteArrayInputStream(input.getBytes());
    System.setOut(new PrintStream(testOutput));
    System.setIn(testInput);
    testReviewSystem.main(new String[] {});
    assertEquals(testOutput.toString(), "What do you want to do?\n" +
            "O = Overview, P = Add Paper, R = Add Review, [num] = Detail of that paper, X = exit\n" +
            "What is the title of the paper?\n" +
            "[Paper added]\n"
            + "What do you want to do?\n" +
            "O = Overview, P = Add Paper, R = Add Review, [num] = Detail of that paper, X = exit\n");
}

When I look at the two strings I'm told they're identical. 当我看到这两个字符串时,我被告知它们是相同的。 在此输入图像描述

It is failing because they are not identical. 失败是因为它们不相同。

Two strings that look identical can be diferent. 看起来相同的两个字符串可能会有所不同。 There are many bytes that can't be representated and shown. 有很多字节无法表示和显示。

For example ascii code 0 and ascii code 1, they'd look identical but they aren't. 例如ascii代码0和ascii代码1,它们看起来相同,但它们不是。

http://www.ascii-code.com/ http://www.ascii-code.com/

I think that you're running your tests on windows and it outputs \\r\\n instead of \\n as line separator. 我认为您正在Windows上运行测试,并且它输出\\r\\n而不是\\n作为行分隔符。 You can try this by changing your assert to the following code. 您可以通过将断言更改为以下代码来尝试此操作。

assertEquals(testOutput.toString(), "What do you want to do?\r\n" +
        "O = Overview, P = Add Paper, R = Add Review, [num] = Detail of that paper, X = exit\r\n" +
        "What is the title of the paper?\r\n" +
        "[Paper added]\r\n"
        + "What do you want to do?\r\n" +
        "O = Overview, P = Add Paper, R = Add Review, [num] = Detail of that paper, X = exit\r\n")

I've written a test library named System Rules that makes testing command-line applications easier. 我编写了一个名为“ 系统规则”的测试库,该库使测试命令行应用程序更加容易。

public class TheTest {
  @Rule
  public final TextFromStandardInputStream systemInMock
    = emptyStandardInputStream();
  @Rule
  public final SystemOutRule systemOutRule
    = new SystemOutRule().enableLog();

  @Test
  public void testAddPaperConfirm() {
    systemInMock.provideLines("P", "The Life of Geoff the Platypus");
    testReviewSystem.main(new String[] {});
    String output = systemOutRule.getLogWithNormalizedLineSeparator();
    assertEquals(output, "What do you want to do?\n" +
        "O = Overview, P = Add Paper, R = Add Review, [num] = Detail of that paper, X = exit\n" +
        "What is the title of the paper?\n" +
        "[Paper added]\n"
        + "What do you want to do?\n" +
        "O = Overview, P = Add Paper, R = Add Review, [num] = Detail of that paper, X = exit\n");
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM