November 8, 2021

Test class for SObject in Salesforce

Sample Apex Class:
public class Sample {
    
    public static void insertRecord( SObject objRecord ) {
        
        try {
            
            insert objRecord;
            
        } Catch( Exception e ) {
            
            system.debug( 'Exception is ' + e.getMessage() );
        }
        
    }
    
}

Test Class:
@isTest
private class SampleTest {
    
    static testMethod void test() {
        
        Sobject objRecord = new Contact( LastName = 'Testing', Email = 'testing@test.com' );
        Test.startTest();
        Sample.insertRecord( objRecord );
        Test.stopTest();
        Contact objContact = [ SELECT LastName FROM Contact LIMIT 1 ];
        system.assertEquals( objContact.LastName, objRecord.get( 'LastName' ) );
        
    }

}

No comments:

Post a Comment