function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
rajubalajirajubalaji 

how to test class for insert part.

Hi Team,

Anyone was have idea about how write a test class for below code.if knows please help me out.

public with sharing class ParentexampleController {    
    public Example Example{get;set;}
    public ActivityData activityData{get;set;}
    public Device deviceInfo{get;set;}
    public MGCDeviceInf MGCDeviceInfo{get;set;}

        List<Example> lstExample = [Select ID from Example where parentid__c=:parentid];
            if (lstExample != null && lstExample.size() > 0) {
                delete lstExample;
            }
            
            List<Activity_Tracker_Information__c> lstActivityTrackerInfo = [Select ID from Activity_Tracker_Information__c where parentid__c=:parentid];
            if (lstActivityTrackerInfo != null && lstActivityTrackerInfo.size() > 0) {
                delete lstActivityTrackerInfo;
            }
            
            Example Example = new Example();
            Example.parentid__c = parentid;
            Example.ReadingLatestDate__c = Example.ReadingLatestDate;
            Example.ReadingLatestDate__c = Example.ReadingLatestDate;
            insert Example;
            
            if (obj.activityTrackerData != null && obj.activityTrackerData.Name != null) {
                List<Activity_Tracker_Information__c> lstActivityTracker = new List<Activity_Tracker_Information__c>();
                Activity_Tracker_Information__c activity = new Activity_Tracker_Information__c();
                activity.parentid__c = parentid;
                activity.Name__c = activityTrackerData.Name;
                activity.Last_Sync_Date__c = activityTrackerData.CreatedDateTime;
                lstActivityTracker.add(activity);
                insert lstActivityTracker;
            }
            
            if (obj.MGCDeviceInfo != null && obj.MGCDeviceInfo.Status != null) {
                if (obj.MGCDeviceInfo.Status == 1) {
                    MGCDeviceInfo.StatusDescription = 'Active';
                }
                else if (obj.MGCDeviceInfo.Status == 2) {
                    MGCDeviceInfo.StatusDescription = 'InActive';
                }
            }
}catch (System.NullPointerException e){
            System.debug('The following exception has occurred: ' + e.getMessage());
        }
    }
    
    public class ParentRequestData{
        public String StarID {get;set;}
        public String parentid {get;set;}
    }


Thanks inadvance,
Raju.
Deepak GerianiDeepak Geriani
Hi,

Referring to your code I can see that you are missing the Try block in your code. if you want to use catch you must use try block first.

For a better understanding of the test class Please refer to the link (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_test.htm) on how to write a test and The Testing Best Practices (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm)

If my answer has helped you to resolve your issue please mark this answer as resolved.
Thanks