读 Spring实战 遇到的问题记录(一)

package soundsystem;

import beanConfig.CDPlayerConfig;
import org.junit.Rule;
import org.junit.Test;
//这个包不在junit依赖中,需要添加spring-test依赖
import org.junit.contrib.java.lang.system.SystemOutRule;
//书中使用了已被废弃的方法
//import org.junit.contrib.java.lang.system.StandardOutputStreamLog;

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 static junit.framework.TestCase.*;
//书中使用Assert,该类已经完全被TestCase取代
//import static junit.framework.Assert.*;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDPlayerConfig.class)
public class CDPlayerTest {

    @Rule
    public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();
    //这里注意enableLog(),第一次使用忘了加导致不能获取到控制台输出

    @Autowired
    private MideaPlayer cdPlayer;

    @Autowired
    private CompactDisc cd;

    @Test
    public void cdShouldNotBeNull() {
        assertNotNull(cd);
    }

    @Test
    public void play() {
        cdPlayer.play();
        assertEquals("Playing Sgt. Pepper's Lonely Hearts Club Band by The Beatles\r\n", systemOutRule.getLog());
        //windows下CRLF使用\r\n表示
    }
}

 

posted @ 2019-04-20 23:35  段誉好帅啊  阅读(414)  评论(0编辑  收藏  举报