久久久久久久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(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 日韩视频一区二区三区 | 国产在线网址 | 特黄特色大片免费视频观看 | 日韩久久精品电影 | 日日摸夜夜添夜夜添精品视频 | 亚洲日韩中文字幕一区 | 日日摸日日添日日躁av | 日韩精品 电影一区 亚洲 | 日本精品999| 欧美人妇做爰xxxⅹ性高电影 | 精品国产成人 | 久久久久久久久久久丰满 | 中文字幕亚洲精品 | 秋霞精品 | 日韩av一区二区在线观看 | 黄色毛片在线观看 | 日韩欧美在线不卡 | 99久久婷婷国产综合精品电影 | 亚洲成人在线免费 | 一区二区三区免费观看 | 一级毛片视频在线观看 | 国产免费一区二区三区 | 久久精品国产一区二区电影 | 国产aa| 欧美 日韩 国产 一区 | 亚洲综合一区二区三区 | 日韩精品一区二区三区免费观看 | 欧美黑人又粗大 | 桃色五月 | av天天干 | 日韩成人在线电影 | 成人亚洲视频 | 麻豆成人在线视频 | 欧美在线一区二区三区 | 亚洲免费在线播放 | 日韩一区二区免费视频 | 91精品国产自产精品男人的天堂 | 色婷婷亚洲国产女人的天堂 | 国产成人自拍一区 | 国产精品久久久久久一区二区三区 | 国产精品高潮呻吟久久 |