Системные правила возвращают другой разделитель строк
У меня есть весенний тестовый класс от Spring in Action 4th
который проверяет простой класс, который печатает что-то для стандартного вывода. Оно использует System-Rules
библиотека включена в junit.
Когда я запускаю тест, он бросил ComparisionException
- единственная разница была разделителем строк. В конце концов мне удалось обойти это, выбрав разделитель строк из System.properties
,
Можно ли как-то настроить System-Rules, чтобы сделать это автоматически? Я использую Windows 7 Professional.
Тестовое задание
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());
}
}
Протестированный класс
@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);
}
}
2 ответа
Системные правила просто возвращают то, что было написано System.out
, Я добавил новый метод log.getLogWithNormalizedLineSeparator()
к системным правилам 1.12.0, которая решает вашу проблему. (Вы должны использовать новое правило SystemOutRule
вместо StandardOutputStreamLog
.)
Это тест из этой книги:
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());
}
}