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

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

阅读更多

UnitTestFirewall中也要做相应的修改,以允许在测试时,定制Clock实例。

 

package shannon.demo;

 

import thirdparty.any.NtpClock;

 

/**

 * <code>UnitTestFirewall</code> is the facility to

 * ease unit test

 * @author Shannon Qian

 */

public final class UnitTestFirewall {

    private static boolean debugging=false;

 

    /**Returns true if it's in debugging mode, else false.

     * @return the debugging

     */

    public static boolean isDebugging() {

       return debugging;

    }

 

    /**Sets Debugging flag as true if it's time to unit test.

     * @param on - the debugging to set, true for on and false

     * for off

     */

    public static void setDebugging(boolean on) {

       UnitTestFirewall.debugging = on;

    }

   

    private final static NtpClock _ntpClock=new NtpClock();

   

    private static class NtpClockWrapper implements Clock {

       public long getTime() {

           return _ntpClock.getTime();

       }

    }

   

    private static NtpClockWrapper ntpClock = null;

    private static Clock clock = null;

   

    /**sets the Clock instance for debugging.

     * If candidate is not null, {@link #getClock()} will

     * return it when debugging is true.

     * @param candidate - the Clock instance for debugging

     */

    public static void presetClock(Clock candidate) {

       clock=candidate;

    }

   

    /**Returns the Clock instance for <code>SystemTimeSynchronizer

     * </code>'s invocation.

     * @return - Clock instance

     */

    public static Clock getClock() {

       if(debugging && clock != null) {

           return clock;

       } else {

              if(ntpClock == null)

                  ntpClock = new NtpClockWrapper();

              return ntpClock;

       }

    }

}

 

(未完待续)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics