第2章Spring Action in Pg。SystemOutRule.getLogを使用したAssertEqualsの失敗
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クラスを実装:春のツール・スイートを使用して40 は、私は次のことを行いましたコンパクトディスク
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の周りで頭を包み込み、これらのクラスを結ぶ限りSpringがやっていることの背後にある目的を取ります。
私はそれを試してみましたが、無駄に。 <ビートルズ[] で...バンド>しかしだった: org.junit.ComparisonFailure:期待を示唆したように改行を追加した後、新たなスタックトレースを読み込む<...ビートルズ[ ] によってバンド> ビートルズの終わりに角括弧は何を意味していますか? – arr
'\ r \ n'でも試しましたか? printlnはOS改行文字を使用しているようです。 – dpr
これはうまくいきました https://stackoverflow.com/questions/9260126/what-are-the-differences-between-char-literals-n-and-r-in-java – arr