Loading
Salesforce から送信されるメールは、承認済ドメインからのみとなります続きを読む

System.Assert と System.AssertEquals の違い

公開日: Oct 13, 2022
説明

System.Assert と System.AssertEquals の違い

  • System.Assert は、テストする条件であるパラメータ (必須) と、その条件が false である場合に表示する別のメッセージ (オプション) という 2 つのパラメータを受け付けます。
  • System.AssertEquals と System.AssertNotEquals はどちらも、最初の 2 つ (必須) が非/等値性をテストされる変数であり、3 番目 (オプション) が、アサートが false になった場合に表示するメッセージである 3 つのパラメータを受け付けます。

両方とも、1 つまたは複数のレコードを使用した「正常系のケースのテスト」と「異常系のケースのテスト」用のテストクラスで使用されます。

System クラス

解決策

System.assert と System.assertEquals を使用して、異常系のサンプルテストクラス (Sample Test Class) でテストを行いましょう。

static testMethod void runNegativeTestCases(){
	User u3 = [select id from User where alias='tuser'];     
	System.RunAs(u3)
	{
		System.debug('Inserting a record with 501 miles... (negative test case)');

		Mileage__c testMiles3 = new Mileage__c( Miles__c = 501, Date__c = System.today() );
		try{

			insert testMiles3;

		}catch (DmlException e){

			//Assert Error Message 

			System.assert( e.getMessage().contains('Insert failed. First exception on ' +'row 0; first error:FIELD_CUSTOM_VALIDATION_EXCEPTION,'+'Mileage request exceeds daily limit(500): [Miles__c]'),e.getMessage() );

			//Assert field 

			System.assertEquals(Mileage__c.Miles__c, e.getDmlFields(0)[0]);

			//Assert Status Code 

			System.assertEquals('FIELD_CUSTOM_VALIDATION_EXCEPTION' , e.getDmlStatusCode(0) );

		} //catch 

} //RunAs(u3)
ナレッジ記事番号

000382121

 
読み込み中
Salesforce Help | Article