AssertEquals使用SystemOutRule.getLog失败

时间:2017-08-15 15:20:05

标签: java spring

Spring in Action的第02章,Pg。 40 使用Spring Tool Suite,我做了以下内容:

MediaPlayer界面

package com.spring.soundsystem;

public interface MediaPlayer {
    void play();
}

CompactDisc接口

package com.spring.soundsystem;

public interface CompactDisc {
    void play();
}

CDPlayer类实现MediaPlayer

package com.spring.soundsystem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CDPlayer implements MediaPlayer {

    private CompactDisc cd;

    @Autowired
    public CDPlayer(CompactDisc cd) {
        this.cd = cd;
    }


    public void play() {
        cd.play();
    }

}

SgtPeppers类实现CompactDisc

package com.spring.soundsystem;

import org.springframework.stereotype.Component;

@Component("lonelyHeartsClub")
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);

    }

}

CDPlayerConfig

package com.spring.soundsystem;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan()
public class CDPlayerConfig {}

CDPlayerTest

package com.spring.soundsystem;

import static 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.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig.class)
public class CDPlayerTest {

    // public final StandardOutputStreamLog log = new StandardOutputStreamLog(); deprecated code in book replace with below
    @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() {
        log.clearLog(); // clears debug that occurred for some reason in log output
        player.play();
        assertEquals(
                "Playing Sgt. Pepper's Lonely Hearts Club Band by The Beatles",
                log.getLog());
    }

}

问题是,当我运行JUnit测试时,出现以下跟踪失败:

org.junit.ComparisonFailure: expected:<... Band by The Beatles[]> but was:<... Band by The Beatles[
]>
    at org.junit.Assert.assertEquals(Assert.java:115)
    at org.junit.Assert.assertEquals(Assert.java:144)
    at com.spring.soundsystem.CDPlayerTest.play(CDPlayerTest.java:36)
...

测试应该根据显示的文字传递,但我想这可能是我理解范围之外的数据类型/内存比较问题?我没有看到任何不必要的空格或字符,所以如果不了解这种日志记录比较的基本原理,它一定是我看不到的东西。

我还有一个额外的问题,如果有人愿意,请从40k脚的角度解释所有这些意味着什么。我仍然试图围绕DI和我们正在做的目的背后的目的,只要将这些类连接在一起。

4 个答案:

答案 0 :(得分:4)

我假设System.out.println在日志消息的末尾添加换行符。这就是你的断言失败的原因。我认为将其更改为可以解决问题:

final String newLine = System.lineSeparator();
assertEquals("Playing Sgt. Pepper's Lonely Hearts Club Band by The Beatles"
    + newLine, log.getLog());

答案 1 :(得分:1)

@Test
    public void play() {
        log.clearLog();
        player.play();
        assertEquals(
                "Playing Sgt. Pepper's Lonely Hearts Club Band by The Beatles\n",
                log.getLogWithNormalizedLineSeparator());
    }

//it works!

答案 2 :(得分:1)

如果您调试应用程序,则在验证String是否从getLog()获得时,assertEquals方法比较两个Object,第一个是期望的对象(由您输入),第二个是从getLog()方法接收的对象。 ),您还将看到它还包含Spring在控制台上启动的日志,这就是为什么它会出错并指示两个对象不同的原因。

答案 3 :(得分:0)

似乎此问题在SystemOutRule assertEquals("some text\n", systemOutRule.getLogWithNormalizedLineSeparator()); data-position中非常普遍。

最容易使用这样的东西:
mouseover