`
shannon977
  • 浏览: 19236 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
最近访客 更多访客>>
社区版块
存档分类
最新评论

谈谈单元测试中的测试桩实践 (1)

阅读更多

写程序的时候,有时可能遇到这样的情况。比如我们开发了一个数据处理模块,被处理的数据需要调用其他模块(由其他团队开发,或者是第三方提供,总之测试的责任不在你),从数据库或者文件或者通过网络从其他进程中获取。为了对该数据处理模块进行单元测试,我们通常得相应的配置起一个数据库或者文件系统,甚至是相关进程,以求正常的得到数据,但这样做的代价往往较大。

这里想讨论一种我以前曾经使用过的简化单元测试的思路。通过接口来封装对外部模块的调用,在单元测试时,用调试实现代替外部实现。myworkfirst指点,又google了一下,才知道这是单元测试里早已成熟的“测试桩”。但我仍然想把我的实践和大家分享一下。

我们用一个简单的例子来说明。比如我实现了一个SystemTimeSynchronizer类,周期性的查询NTP标准时间,和本地系统时间进行比较。

 

/**shannon.demo is the package for the demonstration, in which,

 

 * there's all the codes of unit test target module.

 */

package shannon.demo;

 

import thirdparty.any.NtpClock;

 

/**

 * <code>SystemTimeSynchronizer</code> is our unit test target,

 * which acts as if calibrating the system time firmly in

 * compliance with the standard time.

 * @author Shannon Qian

 */

public class SystemTimeSynchronizer {

    /**Compares the local system time with the standard time.

     * @return - 1 if system time is ahead of standard time,

     * 0 if it's on standard time and -1 if it's behind standard

     * time.

     */

    public int syncTime() {

       long currentTime = new NtpClock().getTime();

       long interval = System.currentTimeMillis()-currentTime;

       if(interval == 0) {

           return 0;

       } else if(interval > 0) {

           return 1;

       } else {

           return -1;

       }

    }

}

 

SystemTimeSynchronizer#syncTime()调用的NtpClock类,属于外部模块。NtpClock#getTime()在这里只是一个示意,说明在没有预设NTP服务器的情况下,它将抛出异常(这和我们在单元测试时实际遇到的情况类似)。但是请你想象其内部实现通过访问预设的NTP服务器获取标准时间。要让NtpClock类正常的运行起来,需要一个NTP服务器,并事先进行比较复杂的设置。

 

/**package thirdparty.any plays the role to contain all the codes

 * as if from thrid party.

 */

package thirdparty.any;

 

/**

 * <code>NtpClock</code> is a demenstrating class for this unit test firewall

 * example. it acts as if a third-party-provided adaptor with access to the

 * NTP server.

 * @author Shannon Qian

 */

public class NtpClock {

   

    /**Returns the standard time from NTP server.

     * @return - the standard time from NTP server

     * @throws IllegalStateException - if there's no NTP server available.

     */

    public long getTime() {

       //if there's no NTP server available.

       throw new IllegalStateException("NTP server is not ready.");

    }

}

 

在不配置NTP服务器的情况下,单元测试肯定会因为异常抛出而中断。为了避免麻烦,我们首先想到的是如果不调用NtpClock就好了。但如果不调用,就无法获取标准时间。这样我们只能另外造一个类,在单元测试时替代NtpClock,能够方便的提供标准时间。新的问题是SystemTimeSynchronizer需要知道在不同时机调用不同的对象-在单元测试时,调用我们自定义的类,而在正常运行时仍然调用NtpClock.

(未完待续,代码太多了,可以到我的博客里看,或者下载zip文件)

分享到:
评论
3 楼 shannon977 2009-06-11  
我觉得我写得还可以......虽然被集体评为新手帖,让我有些气馁!不过没关系,我觉得不把自己的想法贴出来让大家看,就不能知道自己的浅薄,更不能学到很多有用的东西。
我想把我这几天写的,都贴到入门讨论版来。
2 楼 shannon977 2009-06-11  
谢谢,受教了!我去改一下。

在这里发贴收获不小的。
1 楼 myworkfirst 2009-06-11  
    面对这种情况,一般都会写一个简的测试桩,就解决测试功能的问题

相关推荐

Global site tag (gtag.js) - Google Analytics