系统规则返回不同的行分隔符

时间:2015-03-14 08:16:32

标签: java spring junit system-rules

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

当我运行测试时,它抛出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());
    }
}

经过测试的课程

@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 个答案:

答案 0 :(得分:3)

系统规则只返回写入System.out的内容。我向System Rules 1.12.0添加了一个新方法log.getLogWithNormalizedLineSeparator()来解决您的问题。 (您必须使用新规则SystemOutRule而不是StandardOutputStreamLog。)

答案 1 :(得分:0)

这是那本书的考验:

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