Update 35.4.3. OutputCapture.md

master
qibaoguang 2015-02-27 21:26:35 +08:00
parent e27e14da48
commit 782914516b
1 changed files with 20 additions and 0 deletions

View File

@ -0,0 +1,20 @@
### 35.4.3. OutputCapture
OutputCapture是一个JUnit Rule用于捕获System.out和System.err输出。只需简单的将捕获声明为一个@Rule并使用toString()断言:
```java
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.test.OutputCapture;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
public class MyTest {
@Rule
public OutputCapture capture = new OutputCapture();
@Test
public void testName() throws Exception {
System.out.println("Hello World!");
assertThat(capture.toString(), containsString("World"));
}
}
```