Unit Testing with loading Spring Context!

Spring Container - Standalone Java - XML Configuration..

Directory Layout

config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd">
 
    <bean id="sgtPeppersDisc" class="soundsystem.compactDiscs.SgtPeppers"/>
 
    <bean id="compactDiscPlayer" class="soundsystem.players.AiwaDiscPlayerImpl">
        <property name="compactDisc" ref="sgtPeppersDisc"/>
    </bean>
</beans>

*.java
// CompactDisc.java
 
package soundsystem.compactDiscs;
 
/* User: koray@tugay.biz Date: 01/07/15 Time: 11:38 */ 
public interface CompactDisc {
    void play();
}
 
// SgtPeppers.java
 
package soundsystem.compactDiscs;
 
/* User: koray@tugay.biz Date: 01/07/15 Time: 11:39 */ 
public class SgtPeppers implements CompactDisc { 
    private String title = "Sgt. Pepper's Lonely Club Band";
    private String artist = "The Beatles";
    @Override
    public void play() {
        System.out.println("Playing " + title + " by " + artist);
    }
}
 
// CompactDiscPlayer.java
 
package soundsystem.players;
 
/* User: koray@tugay.biz Date: 01/07/15 Time: 13:01 */ 
public interface CompactDiscPlayer {
    public void startCompactDiscPlayer();
}
 
// AiwaDiscPlayerImpl
 
package soundsystem.players;
 
import org.springframework.beans.factory.annotation.Required;
import soundsystem.compactDiscs.CompactDisc;
 
/* User: koray@tugay.biz Date: 01/07/15 Time: 13:02 */ 
public class AiwaDiscPlayerImpl implements CompactDiscPlayer {
    private CompactDisc compactDisc;
    private String brandOfCDPlayer = "Aiwa";
    @Override
    public void startCompactDiscPlayer() {
        System.out.println("Playing on : " + brandOfCDPlayer);
        compactDisc.play();
    }
    @Required
    public void setCompactDisc(CompactDisc compactDisc) {
        this.compactDisc = compactDisc;
    }
}
 
// MainClass.java
 
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import soundsystem.players.CompactDiscPlayer;
 
/* User: koray@tugay.biz Date: 01/07/15 Time: 13:15 */ 
public class MainClass { 
    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext 
            = new ClassPathXmlApplicationContext("config.xml");
        CompactDiscPlayer compactDiscPlayer 
            = (CompactDiscPlayer) applicationContext.getBean("compactDiscPlayer");
        compactDiscPlayer.startCompactDiscPlayer();
    }
}
 
// CDPlayerTest.java
package soundsystem;
 
import org.junit.Assert; 
import org.junit.Test;
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;
import soundsystem.compactDiscs.CompactDisc;
 
/* User: koray@tugay.biz Date: 01/07/15 Time: 11:45 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:config.xml"})
public class CDPlayerTest {
 
    @Autowired
    private CompactDisc compactDisc;
 
    @Test
    public void cdShouldNotBeNull() {
        Assert.assertNotNull(compactDisc);
    }
}
 
// AiwaDiscPlayerImplTest.java
 
package soundsystem.players;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import soundsystem.compactDiscs.CompactDisc;
 
/* User: koray@tugay.biz Date: 01/07/15 Time: 13:06 */
@RunWith(MockitoJUnitRunner.class)
public class AiwaDiscPlayerImplTest {
 
    @Mock
    CompactDisc compactDisc;
 
    @InjectMocks
    CompactDiscPlayer compactDiscPlayer = new AiwaDiscPlayerImpl();
 
    @Test
    public void testStartCompactDiscPlayer() {
        compactDiscPlayer.startCompactDiscPlayer();
        Mockito.verify(compactDisc, Mockito.times(1)).play();
    }
}