Add OutputCapture test class

This commit is contained in:
Phillip Webb
2013-08-05 12:15:33 -07:00
parent 174427881a
commit 4038b39496
10 changed files with 186 additions and 198 deletions

View File

@@ -16,13 +16,11 @@
package org.springframework.boot.sample.simple;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.sample.simple.SampleSimpleApplication;
import org.springframework.boot.OutputCapture;
import static org.junit.Assert.assertTrue;
@@ -34,17 +32,13 @@ import static org.junit.Assert.assertTrue;
*/
public class SampleSimpleApplicationTests {
private PrintStream savedOutput;
private ByteArrayOutputStream output;
@Rule
public OutputCapture outputCapture = new OutputCapture();
private String profiles;
@Before
public void init() {
this.savedOutput = System.out;
this.output = new ByteArrayOutputStream();
System.setOut(new PrintStream(this.output));
this.profiles = System.getProperty("spring.profiles.active");
}
@@ -56,24 +50,19 @@ public class SampleSimpleApplicationTests {
else {
System.clearProperty("spring.profiles.active");
}
System.setOut(this.savedOutput);
}
private String getOutput() {
return this.output.toString();
}
@Test
public void testDefaultSettings() throws Exception {
SampleSimpleApplication.main(new String[0]);
String output = getOutput();
String output = this.outputCapture.toString();
assertTrue("Wrong output: " + output, output.contains("Hello Phil"));
}
@Test
public void testCommandLineOverrides() throws Exception {
SampleSimpleApplication.main(new String[] { "--name=Gordon" });
String output = getOutput();
String output = this.outputCapture.toString();
assertTrue("Wrong output: " + output, output.contains("Hello Gordon"));
}