1

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. enter image description here

kryger
  • 12,906
  • 8
  • 44
  • 65
James Burton
  • 94
  • 1
  • 9
  • 1
    Hint: stay away from assertEquals. Simply check out its big brother **assertThat**. That one is typically far better in comparing values and telling you what is not matching. – GhostCat Mar 17 '16 at 01:17
  • 1
    Your test is missing some crucial info that would let us try to reproduce the problem.. Can you update to include it? – Krease Mar 17 '16 at 01:24
  • 2
    Perhaps the actual output uses `\r\n` for newlines? – Andreas Mar 17 '16 at 01:25
  • @Chris I don't think I can share the code that I'm testing because I didn't write it. – James Burton Mar 17 '16 at 02:08

2 Answers2

1

I think that you're running your tests on windows and it outputs \r\n instead of \n as line separator. 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");
  }
}
Stefan Birkner
  • 24,059
  • 12
  • 57
  • 72
-2

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.

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

Rafael Lucena
  • 635
  • 1
  • 6
  • 9