Test class for SObject in Salesforce

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 = ‘[email protected]’ );
        Test.startTest();
        Sample.insertRecord( objRecord );
        Test.stopTest();
        Contact objContact = [ SELECT LastName FROM Contact LIMIT 1 ];
        system.assertEquals( objContact.LastName, objRecord.get( ‘LastName’ ) );
        
    }

}

Leave a Reply