public class WelcomePage {
private String welcomeMessage;
public WelcomePage(String message) {
this.welcomeMessage = message;
}
public void showWelcomeMessages() {
System.out.println(this.welcomeMessage);
}
}
编写测试
public class WelcomePageTest {
private PrintStream console = null;
private ByteArrayOutputStream bytes = null;
@Before
public void setUp() {
bytes = new ByteArrayOutputStream();//把标准输出指定到ByteArrayOutputStream中
console = System.out;// 获取System.out 输出流的句柄
System.setOut(new PrintStream(bytes));//将原本输出到控制台Console的字符流重定向到bytes
}
@After
public void tearDown() {
System.setOut(console);
}
@Test
public void shouldPrintWelcomeMassages() {
String massage= "Welocme to Test.";
WelcomePage welcomePage