Skip to content

Commit b5fc68d

Browse files
committed
Merge pull request spring-projects#8490 from schlauergerd:spring-projectsgh-8390
* pr/8490: Polish contribution Introduce reset method to OutputCapture
2 parents feb1ab3 + c8ac94b commit b5fc68d

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

spring-boot-test/src/main/java/org/springframework/boot/test/rule/OutputCapture.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -91,6 +91,13 @@ protected void releaseOutput() {
9191
this.copy = null;
9292
}
9393

94+
/**
95+
* Discard all currently accumulated output.
96+
*/
97+
public void reset() {
98+
this.copy.reset();
99+
}
100+
94101
public void flush() {
95102
try {
96103
this.captureOut.flush();
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.rule;
18+
19+
import org.junit.Rule;
20+
import org.junit.Test;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
24+
/**
25+
* Tests for {@link OutputCapture}.
26+
*
27+
* @author Roland Weisleder
28+
*/
29+
public class OutputCaptureTests {
30+
31+
@Rule
32+
public OutputCapture outputCapture = new OutputCapture();
33+
34+
@Test
35+
public void toStringShouldReturnAllCapturedOutput() throws Exception {
36+
System.out.println("Hello World");
37+
assertThat(this.outputCapture.toString()).contains("Hello World");
38+
}
39+
40+
@Test
41+
public void reset() throws Exception {
42+
System.out.println("Hello");
43+
this.outputCapture.reset();
44+
System.out.println("World");
45+
assertThat(this.outputCapture.toString()).doesNotContain("Hello")
46+
.contains("World");
47+
}
48+
49+
}

0 commit comments

Comments
 (0)