久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

處理具有當前時間條件的單元測試

Handling unit tests with a condition on the current time(處理具有當前時間條件的單元測試)
本文介紹了處理具有當前時間條件的單元測試的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試為我正在處理的項目中的一些實用程序類設置單元測試,其中一個類(包含許可信息)具有一種基于當前時間進行某些確定的方法.

I'm taking a stab at setting up unit tests for some utility classes in a project I'm working on, and one of the classes (contains licensing info) has a method that does some determination based on the current time.

即許可證包含到期日期,并且許可證字符串驗證該日期,但查看許可證是否過期的實際邏輯是基于當前時間.

i.e. the license contains an expiry date, and the license string validates that date, but the actual logic to see if the license is expired is based on the current time.

public boolean isValid()
{
    return isLicenseStringValid() && !isExpired();
}

public boolean isExpired()
{
    Date expiry = getExpiryDate();
    if( expiry == null ) {
        return false;
    }

    Date now = new Date();

    return now.after( expiry );
}

所以,我不確定該怎么做,因為new Date()"不是靜態標準.

So, I'm not sure what to do, since the 'new Date()' thing isn't a static criterion.

  1. 我是否應該不費心去測試 'isValid',而只測試 'isLicenseStringValid()' 和 'getExpiryDate()' 函數?
  2. 我是否只是在測試中使用過期時間很長的許可證密鑰,這樣我就可以在過期時換工作?
  3. 我是否嘗試將 'new Date()' 模擬為一些 'getCurrentTime()' 方法,以便我可以偽造現在幾點?

其他人通常如何處理時間條件測試?

What do others normally do with tests that are time-conditional?

推薦答案

一定要mock out new Date().

Definitely mock out new Date().

使用 getCurrentTime() 方法或類似方法創建一個 Clock 接口.這樣你就可以有一個 FakeClock 用于測試和一個 SystemClock 使用 System.currentTimeMillis() 或其他.

Create a Clock interface with a getCurrentTime() method or something similar. That way you can have a FakeClock for testing and a SystemClock which uses System.currentTimeMillis() or whatever.

我已經做過很多次了——效果非常好.這也是合乎邏輯的——實際上你需要一個當前時間服務",所以它應該像任何其他依賴項一樣被注入.

I've done this a number of times - it's worked extremely well. It's logical too - effectively you require a "current time service" so that should be injected like any other dependency.

這篇關于處理具有當前時間條件的單元測試的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to mock super reference (on super class)?(如何模擬超級參考(在超級類上)?)
Java mock database connection(Java 模擬數據庫連接)
Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無法投射模擬)
Set value to mocked object but get null(將值設置為模擬對象但獲取 null)
How to mock DriverManager.getConnection(...)?(如何模擬 DriverManager.getConnection(...)?)
Mockito; verify method was called with list, ignore order of elements in list(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 国产又粗又大又长 | 9.1成人看片免费版 国产草草影院 | 国产黄在线观看 | 伊人网站 | 成人av网站在线观看 | a级片在线免费观看 | 国产一级一片免费播放放a 免费国产视频 | 国产黄色精品 | 亚洲综合色网 | 天天操天天操 | a一级黄色片 | 91狠狠干 | 日韩性视频 | 欧美精品在线视频 | 日韩视频在线观看免费 | 性色av一区二区三区 | 激情五月综合色婷婷一区二区 | 国产女人18毛片18精品 | 久久av网 | 亚洲一区成人 | 国产伦精品一区二区三区视频我 | 黄色免费av | 好吊视频一区二区三区四区 | 欧美日韩免费视频 | 中文在线字幕观看 | 久久久久97 | 国产一区二区在线观看视频 | 性欧美精品 | 午夜看看 | 91超碰在线观看 | 久久九 | 夜夜操夜夜操 | 国产在线不卡 | 午夜精品久久久久 | 国产女人高潮视频 | 欧美精品亚洲 | 天天爱夜夜操 | 亚洲激情综合网 | 久久久成人免费视频 | 成人高清视频在线观看 | 欧美一级片免费 |