@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig.class)
public class CDPlayerTest {
@Rule
public final SystemOutRule systemOutRule = new SystemOutRule();
@Autowired
private CompactDisc cd;
@Autowired
private SgtPeppers sg;
@Test
public void cdShouldNotBeNull() {
Assert.notNull(cd, "is not null");
}
@Test
public void play() {
sg.play();
Assert.hasText(
"Playing Somthing just like this. by The Chainsmokers;ColdPlay\n",
systemOutRule.getLog()
);
}
}
最近在使用 junit 4.11 需要用StandardOutputStreamLog的 jar包system-rules 1.16.0(如果使用maven的话可以直接去库里面检索一下就可以找到 传送门 http://mvnrepository.com/artifact/com.github.stefanbirkner/system-rules/1.16.0)。但是引用之后发现这个方法已经不建议被使用了于是上网搜索了一下发现发现了org.junit.contrib.java.lang.system.SystemOutRule 这个。作用同样是基于控制台的输出log去做断言。 给大家上一个图吧,引用的东西比较多就不写代码了。
其中引用的org.springframework.util.Assert这个是spring给提供得一个通用类来完成断言。