Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用SystemOutRule.getLog的AssertEquals失败_Java_Spring - Fatal编程技术网

Java 使用SystemOutRule.getLog的AssertEquals失败

Java 使用SystemOutRule.getLog的AssertEquals失败,java,spring,Java,Spring,《弹簧作用》第02章,第40页 使用Spring工具套件,我做了以下工作: 媒体播放器接口 package com.spring.soundsystem; public interface MediaPlayer { void play(); } package com.spring.soundsystem; public interface CompactDisc { void play(); } 光盘接口 package com.spring.soundsystem;

《弹簧作用》第02章,第40页 使用Spring工具套件,我做了以下工作:

媒体播放器接口

package com.spring.soundsystem;

public interface MediaPlayer {
    void play();
}
package com.spring.soundsystem;

public interface CompactDisc {
    void play();
}
光盘接口

package com.spring.soundsystem;

public interface MediaPlayer {
    void play();
}
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)
...
org.junit.ComparisonFailure:应为:但为:
位于org.junit.Assert.assertEquals(Assert.java:115)
位于org.junit.Assert.assertEquals(Assert.java:144)
在com.spring.soundsystem.CDPlayerTest.play上(CDPlayerTest.java:36)
...
根据显示的文本,测试应该通过,但我认为这可能是一个超出我理解范围的数据类型/内存比较问题?我看不到任何不必要的空格或字符,所以如果不了解日志比较的基本原理,我肯定看不到


我还有一个问题,如果有人愿意的话,请解释一下从4万英尺的角度来看这一切意味着什么。我仍在试图了解DI和Spring将这些类连接在一起的目的。

我假设
System.out.println
在日志消息的末尾添加了一个新行。这就是你的断言失败的原因。我认为把它改成这个,可以达到以下目的:

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

似乎在for
SystemOutRule
中提到了这个问题

使用这样的东西可能最容易:

assertEquals(“一些文本\n”,systemOutRule.getLogWithNormalizedLineSeparator())

如果调试应用程序,assertEquals方法会比较两个对象:第一个是预期的对象(由您输入),第二个是从getLog()方法接收的对象,当验证从getLog()获取的字符串时,您将看到它还包括Spring在控制台上启动的日志,这就是它给出错误并指出两个对象不同的原因。

我试过了,但没有用。按建议添加新行后的新堆栈跟踪如下:org.junit.ComparisonFailure:预期:但是:甲壳虫乐队结尾的括号是什么意思?您是否也尝试过
\r\n
?println似乎使用了OS换行符。这起作用了@arr I通过使用系统属性
line.separator
稍微调整了示例,使其与OS无关,否则,如果在linux或macOS上执行,测试将失败。