Loading

Difference between System.Assert and System.AssertEquals

게시 일자: Oct 13, 2022
상세 설명

Difference between System.Assert vs System.AssertEquals

  • System.Assert accepts two parameters, one (mandatory) which is the condition to test for and the other a message (optional) to display should that condition be false.
  • System.AssertEquals and System.AssertNotEquals both accepts three parameters; the first two (mandatory) are the variables that will be tested for in/equality and the third (optional) is the message to display if the assert results in false.

Both are used in Test Class for “Positive Case Testing” and “Negative Case Testing” with single and multiple records.

System Class

솔루션

Let’s use System.assert and System.assertEquals to do the testing in sample Negative 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)
Knowledge 기사 번호

000382121

 
로드 중
Salesforce Help | Article