Java 系统规则返回不同的行分隔符
我有一个来自Java 系统规则返回不同的行分隔符,java,spring,junit,system-rules,Java,Spring,Junit,System Rules,我有一个来自spring in Action 4th的spring测试类,它测试将某些内容打印到标准输出的简单类。它使用并入junit的系统规则库 当我运行测试时,它抛出了comparisonexception——唯一的区别是行分隔符。最后,我设法通过从System.properties获取行分隔符来绕过它 是否可以通过某种方式配置系统规则使其自动运行? 我正在使用Windows7专业版 测试 import static org.junit.Assert.*; import org.junit
spring in Action 4th
的spring测试类,它测试将某些内容打印到标准输出的简单类。它使用并入junit的系统规则
库
当我运行测试时,它抛出了comparisonexception
——唯一的区别是行分隔符。最后,我设法通过从System.properties
获取行分隔符来绕过它
是否可以通过某种方式配置系统规则使其自动运行?
我正在使用Windows7专业版
测试
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);
}
}
系统规则只返回写入
System.out
的内容。我添加了一个新方法log.getLogWithNormalizedLineSeparator()
,以解决您的问题。(您必须使用新规则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());
}
}
是否可以通过某种方式配置系统规则使其自动运行?
。。。自动做什么?您的测试play()
在Windows系统上无法通过。