2

I have a spring test class from Spring in Action 4th which tests simple class that prints something to standard output. It uses System-Rules library incorporated into junit.

When I run the test it throwed ComparisionException - the only difference was line separator. Eventually I managed to bypass it by fetching line separator from System.properties.

Is it somehow possible to configure System-Rules to make it automatically ? I'm using windows 7 professional.

Test

import static org.junit.Assert.*;

import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.StandardOutputStreamLog;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:META-INF/spring/soundsystem.xml")
public class CDPlayerXMLConfigTest {

    private String newLine = System.getProperty("line.separator");//This will retrieve line separator dependent on OS.

    @Rule
    public final StandardOutputStreamLog log = new StandardOutputStreamLog();

    @Autowired
    private MediaPlayer player;

    @Autowired
    private CompactDisc cd;

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

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

    @Test //Will pass
    public void play2() {
        player.play();
        assertEquals("Playing Sgt. Pepper's Lonely Hearts Club Band by The Beatles" + newLine, log.getLog());
    }
}

Tested class

@Component
public class SgtPeppers implements CompactDisc {

    private String title = "Sgt. Pepper's Lonely Hearts Club Band";
    private String artist = "The Beatles";

    public void play() {
        System.out.println("Playing " + title + " by " + artist);
    }

}
ashur
  • 4,177
  • 14
  • 53
  • 85
  • `Is it somehow possible to configure System-Rules to make it automatically?` ... to make *what* automatically? Your test `play()` won't pass on a Windows system. – Tom Mar 14 '15 at 08:22

2 Answers2

3

System Rules just returns what has been written to System.out. I added a new method log.getLogWithNormalizedLineSeparator() to System Rules 1.12.0 that solves your problem. (You have to use the new rule SystemOutRule instead of StandardOutputStreamLog.)

Stefan Birkner
  • 24,059
  • 12
  • 57
  • 72
0

This is test from that book:

package com.springbook.stereo;

import com.springbook.CDPlayerConfig;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemOutRule;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.Assert.assertNotNull;

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

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

    @Autowired
    private MediaPlayer player;

    @Autowired
    private CompactDisc cd;

    @Test
    public void cdShouldNotBeNull() {
        assertNotNull(cd);
    }
    @Test
    public void play(){
        player.play();
        Assert.assertEquals("Playing Sgt. Papper's Lonely Hearts Club Band, band The Beatles\n", log.getLogWithNormalizedLineSeparator());
    }
}
RomanP
  • 1
  • 1