0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

APEXテストクラスでdatetime.now()の検証

Posted at

標準項目【成年月日(CreatedDate)】の場合

apexクラスでオブジェクトのdatetime項目にdatetime.now()を挿入した場合検証
オブジェクトの項目【成年月日(CreatedDate)】の場合、テストクラスのsetCreatedDate使用してください。

カスタム項目の型が【日付/時間】の場合

カスタム項目の型が【日付/時間】の場合、salesforceシステムのリソース状況により実際テストクラス走行時間も変わりますため、、直接equal検証する方法がないですが範囲検証できます。一定範囲内で収める方法です。(beforce<=datetime.now<=after)

【注意】

オブジェクトに挿入する際にミリ秒をクリアされるため(beforce.getTime()-1000<=datetime.now.getTime()<=after.getTime())
image.png

実際テストクラスの書き方

long before = datetime.now().getTime()-1000;
DataCreate data = new DataCreate ();
Test.startTest();
data.DataInsert();
Test.stopTest();
list<TEST__c> t = [select id,name,datetime__c from TEST__c];
long after = datetime.now().getTime();
 System.assertEquals(true, before <=t[0].datetime__c.getTime() &&
                               t[0].datetime__c.getTime()<=after);
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?