यूनिट परीक्षण के लिए सबसे अच्छा काम करने वाला एक दृष्टिकोण यह है कि वर्तमान समय आपके कोड में किस प्रकार पहुंचा जा सकता है।
System.currentTimeMillis() को कॉल करने के बजाय सीधे आप अपना स्वयं का कार्यान्वयन प्रदान कर सकते हैं।
कुछ इस (छद्म कोड) की तरह:
class MyTime {
private static TimeInterface CurrentTimeInterface = new DefaultTimeImpl();
public setTimeInterface(TimeInterface timeInterface) {
CurrentTimeInterface = timeInterface;
}
public int getTime() { CurrentTimeInterface.getTime(); }
}
class DefaultTimeImpl implements TimeInterface {
public int getTime() { return System.currentTimeMillis(); }
}
class TestTimeImpl implements TimeInterface {
private int CurrentTimeMillis = 0;
public int getTime() { return CurrentTimeMillis; }
public void setTime(int timeMillis) { CurrentTimeMillis = timeMillis}
public void sleep(int millis) { CurrentTimeMillis += millis; }
}
फिर, हर जगह आप System.getTimeMillis कहा जाता है |() के बजाय MyTime.getTime फोन()। जब आप अपने कोड का परीक्षण कर रहे हैं तो बस एक टेस्टटाइमआईएमएल बनाएं और MyTime.setTimeInterface (testTimeImpl) पर कॉल करें। फिर जब आप कॉल testTimeImpl.sleep() या testTimeImpl.setTime() को अग्रिम करने के लिए समय चाहते हैं।
यह आपको मिलीसेकंड तक किसी भी समय अनुकरण करने की अनुमति देता है।
स्रोत
2009-03-08 19:44:35
क्या प्रोग्राम सिस्टम कॉल के साथ "समय" और "तिथि" कॉल करता है? –
प्रत्येक स्क्रिप्ट को अलग-अलग समय निर्धारित करने के बारे में क्या है :) –
क्या यह प्रोग्राम किसी डेटाबेस या किसी अन्य बाहरी प्रोग्राम पर निर्भर करता है? – jmucchiello