Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package 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;
- }
- @Override
- public void play() {
- cd.play();
- }
- }
- --------------------------------------------------------------------------------------------------------------------------
- package soundsystem;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- //@ComponentScan
- public class CDPlayerConfig {
- }
- --------------------------------------------------------------------------------------------------------------------------
- package soundsystem;
- public interface CompactDisc {
- void play();
- }
- --------------------------------------------------------------------------------------------------------------------------
- package soundsystem;
- public interface MediaPlayer {
- void play();
- }
- --------------------------------------------------------------------------------------------------------------------------
- package soundsystem;
- import org.springframework.stereotype.Component;
- @Component
- public class SgtPeppers implements CompactDisc {
- private String title = "Sgt. Pepper's Lonely Hearts Club Band";
- private String artist = "The Beatles";
- @Override
- public void play() {
- System.out.println("Odtwarzam utwór " + title + " artysty " + artist);
- }
- }
- --------------------------------------------------------------------------------------------------------------------------
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
- <!--<context:annotation-config />-->
- <context:component-scan base-package="soundsystem"/>
- </beans>
- --------------------------------------------------------------------------------------------------------------------------
- package soundsystem;
- 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.CDPlayerConfig;
- import soundsystem.CompactDisc;
- import static org.junit.Assert.*;
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(classes = CDPlayerConfig.class)//konfiguracja kontekstu ma zostac wczytana z tej klasy
- public class CDPlayerTest {
- @Autowired
- private CompactDisc cd;
- @Test
- public void cdShouldBeNull() {
- assertNotNull(cd);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment