您的位置:首页 > 其它

软件测试(四)——Graph Coverage 作业

2016-03-30 01:26 225 查看
题目源程序

package com.Primes;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import junit.framework.TestCase;

public class PrimesTest extends TestCase {

private Primes prime = null;
PrintStream console = null;          // 声明(为null):输出流 (字符设备) console
ByteArrayOutputStream bytes = null;  // 声明(为null):bytes 用于缓存console 重定向过来的字符流

protected void setUp() throws Exception {
super.setUp();
prime = new Primes();
bytes = new ByteArrayOutputStream();    // 分配空间
console = System.out;                   // 获取System.out 输出流的句柄
System.setOut(new PrintStream(bytes));  // 将原本输出到控制台Console的字符流 重定向 到 bytes
}

protected void tearDown() throws Exception {
super.tearDown();
System.setOut(console);
}

public void testPrintPrimes() {
String s = new String("Prime: 2\r\nPrime: 3\r\nPrime: 5\r\n");    // 注意:控制台的换行,这里用 '\n' 表示
prime.printPrimes(3);
assertEquals(s, bytes.toString());          // bytes.toString() 作用是将 bytes内容 转换为字符流
}

}


PrimesTest
结果:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: