Chapter 02 of Spring in Action, Pg. 40 Using Spring Tool Suite, I've made the following:
MediaPlayer Interface
package com.spring.soundsystem;
public interface MediaPlayer {
void play();
}
CompactDisc Interface
package com.spring.soundsystem;
public interface CompactDisc {
void play();
}
CDPlayer Class Implements 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 Class Implements 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());
}
}
The trouble is, when I run the JUnit test there is a failure with the following trace:
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)
...
The test should pass according to the text displayed but I am thinking that maybe this is a data type/memory comparison issue outside of the realm of my understanding? I don't see any unnecessary spaces or characters out of place so it must be something I simply cannot see without understanding the fundamentals of how this logging comparison works.
I also have an additional question, if anyone is willing, to please explain what all of this means from a 40k foot viewpoint. I am still trying to wrap my head around DI and the purpose behind what Spring is doing as far as wiring these classes together.