samedi 14 mars 2015

System rules returns different line separator

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);
}

}

Aucun commentaire:

Enregistrer un commentaire