系统规则 returns 不同的行分隔符

System rules returns different line separator

我有一个来自 Spring in Action 4th 的 spring 测试 class,它测试简单的 class,将一些东西打印到标准输出。它使用合并到 junit 中的 System-Rules 库。

当我 运行 测试时它抛出 ComparisionException - 唯一的区别是行分隔符。最终我设法通过从 System.properties 获取行分隔符来绕过它。

是否可以通过某种方式配置系统规则使其自动生成? 我正在使用 windows 7 专业版。

测试

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

已测试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);
    }

}

系统规则只是 returns 已写入 System.out 的内容。我在 System Rules 1.12.0 中添加了一个新方法 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());
    }
}