单元测试的威力
来源:互联网 发布:锦衣卫同知 编辑:程序博客网 时间:2024/04/27 15:18
为一段代码补充单元测试后出现了让人郁闷的red bar和蓝色的Failures,这是一段中古时期的代码,读取.properties文件并返回Properties句柄:
- public class LoadProp {
- /**
- * 读取.properties配置文件,返回Properties
- * @param path 文件路径
- * @return Properties
- */
- public Properties getProp(String path) {
- InputStream inStream = null;
- try {
- inStream = getInStream(path);
- Properties prop = new Properties();
- prop.load(inStream);
- inStream.close();
- return prop;
- } catch (IOException e) {
- e.printStackTrace();
- return null;
- } finally {
- if(inStream != null) {
- try {
- inStream.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- protected InputStream getInStream(String path) {
- return this.getClass().getClassLoader().getResourceAsStream(path);
- }
- }
当然,这段代码还有很大的改进余地,可以使用ICO和method factory进行重构,抛开其结构优劣不提,仅对功能进行测试,以下是单元测试代码:
1、MockInputStream
- public class MockInputStream extends InputStream {
- private int position = 0;
- private int closeCount = 0;
- private String buffer;
- public void setBuffer(String buffer) {
- this.buffer = buffer;
- }
- public int read() throws IOException {
- if(position == this.buffer.length()) {
- return -1;
- }
- return this.buffer.charAt(position++);
- }
- public void close() throws IOException {
- this.closeCount++;
- super.close();
- }
- public void verify() throws AssertionFailedError {
- if(closeCount != 1) {
- throw new AssertionFailedError("close() should have been called once and once only!" +
- " This close is " + closeCount);
- }
- }
- }
2、TestTableLoadProp
- public class TestTableLoadProp extends LoadProp {
- private InputStream inputStream;
- public InputStream getInStream(String path) {
- return inputStream;
- }
- public void setInputStream(InputStream inputStream) {
- this.inputStream = inputStream;
- }
- }
3、TestLoadProp
- public class TestLoadProp extends TestCase {
- private MockInputStream mockInputStream;
- private TestTableLoadProp prop;
- public void setUp() {
- mockInputStream = new MockInputStream();
- mockInputStream.setBuffer("invoiceManager.roleId = 5862");
- prop = new TestTableLoadProp();
- prop.setInputStream(mockInputStream);
- }
- public void testGetProp() {
- this.assertNotNull(prop.getProp(Variable.FREEZETIME));
- }
- public void tearDown() {
- mockInputStream.verify();
- }
- }
运行TestLoadProp将会提示测试失败:“close() should have been called once and once only! This close is 2”
问题再明显不过。
这个问题很隐蔽,而且并非人人都喜欢掘墓,挖出中古时期的代码,由此可见单元测试的威力。
- 单元测试的威力
- 分区的威力(翻译)
- EnumChildWindows的威力
- 二锅头的威力
- EnumChildWindows的威力
- bat的威力
- 李开复:算法的威力
- 李开复:算法的威力
- 复利的威力
- 腾讯的威力
- 分区的威力(翻译)
- WSAAsyncSelect的威力
- QQ机器人的威力
- 调试器的威力
- 纯文本的威力
- 计算的威力
- XSS的威力
- 计算机科学基础理论的威力
- eco 中怎样在查询和expression中使用类实例变量
- 网站发布成功,在测试机上报运行时错误,发现不明确的匹配
- 转 devexpress 的一些用法
- devexpress的gridcontrol控件用法搜集
- 在devexpress中表格的打印时,怎样选择是否打印组的折叠与否
- 单元测试的威力
- 关于Oracle取整的函数
- log4net 将不同级别的信息写入不同的日志文件
- cronolog的安装配置
- 今天参加了清华和nVidia合办的Cuda培训班
- devexpress的表格中当某条件不满足时某几列只读
- Socket中select()的用法
- gridcontrol 中怎样自定义totalsummary 计算过程
- gdb 使用FAQ