mock object 与真实对象相比,用来构造测试场景。
1. 一个实例
一个闹钟根据时间来进行提醒服务,如果过了下午5点钟就播放音频文件提醒大家下班了,如果我们要利用真实的对象来测试的话就只能苦苦等到下午五点,然后把耳朵放在音箱旁,我们应该利用mock对象 来进行测试,这样我们就可以模拟控制时间了,而不用苦苦等待时钟转到下午5点钟了。
Environmental:抽象类
public abstract class Environmental { private boolean playedWave = false; public abstract long getTime(); public void playWavFile(String filename){ playedWave = true; } public boolean wasPlayedWave() { return playedWave; } public void resetWave(){ playedWave = false; }}
银行实现类:
public class BankEnvironment extends Environmental { public long getTime() { return System.currentTimeMillis(); }}
mock 银行对象:
public class MockBankEnvironment extends Environmental { private long currentTime; public long getTime() { return currentTime; } public void setTime(long time) { currentTime = time; }}