3

I have a JUnit Test that compares two strings:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDPlayerConfig.class)
public class CDPlayerTest {

    @Rule
    public final SystemOutRule log = new SystemOutRule().enableLog();

    @Autowired
    private CompactDisc cd;

    @Autowired
    private MediaPlayer player;

    @Test
    public void cdShouldNotBeNull() {
        assertNotNull(cd);
    }

    @Test
    public void play() {
        player.play();
        assertEquals("Playing title Sgt. Pepper's Lonely Hearts Club Band by artist The Beatles\n",
                log.getLog());
    }
}

I'm getting a org.junit.ComparisonFailure exception on the second test. However, IntelliJ shows contents are identical. What am I missing?

EDIT: Added back the \n at the end of the expected string.

enter image description here

Craig
  • 2,286
  • 3
  • 24
  • 37
Rajkishan Swami
  • 3,569
  • 10
  • 48
  • 68

2 Answers2

5

You have different end line sign (LF vs CRLF)

Marcin Szymczak
  • 11,199
  • 5
  • 55
  • 63
5

Try adding a log.getLog().trim() and removing the \n from the end of your expected string.

Craig
  • 2,286
  • 3
  • 24
  • 37
  • Did but same error. The Expected is from `System.out.println("")`, so the `\n` is there but where is the CR coming from? – Rajkishan Swami Sep 04 '15 at 07:37
  • @Rajkishan I was just editing my answer when I got your response :-P. Yes, you'll have to remove the `\n` from the expected string for the comparison to work. – Craig Sep 04 '15 at 07:38