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
SFDC_LearnerSFDC_Learner 

Code Coverage

 
public void m1(){
String fContent =fbody.toString();
        List<String> Rows = fContent.split('\n');
        DataLoad__c obj;
        List<DataLoad__c> lstD = new List<DataLoad__c>();
        for(Integer i=0;i<Rows.size();i++){
            List<String> ColsData = Rows[i].split(',');
            obj = new DataLoad__c(name=ColsData[0],city__c = ColsData[1],Country__c = ColsData[2]);
            lstD.add(obj);
        }
        system.debug('----> lstD'+lstD);
        insert lstD; 
}

  Can you please paste the test class for the above code

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

 

You have to simply create the instance of class inside the test method and call the method through the instance of class.

 

public class TestCls

{

    public void m1()

    {

   

    String fContent =fbody.toString();

        List<String> Rows = fContent.split('\n');

        DataLoad__c obj;

        List<DataLoad__c> lstD = new List<DataLoad__c>();

        for(Integer i=0;i<Rows.size();i++){

            List<String> ColsData = Rows[i].split(',');

            obj = new DataLoad__c(name=ColsData[0],city__c = ColsData[1],Country__c = ColsData[2]);

            lstD.add(obj);

        }

        system.debug('----> lstD'+lstD);

        insert lstD;

        }

        static testmethod void TestCoverage()

        {

        TestCls t=new TestCls();

        t.m1();

        }

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

You have to simply create the instance of class inside the test method and call the method through the instance of class.

 

public class TestCls

{

    public void m1()

    {

   

    String fContent =fbody.toString();

        List<String> Rows = fContent.split('\n');

        DataLoad__c obj;

        List<DataLoad__c> lstD = new List<DataLoad__c>();

        for(Integer i=0;i<Rows.size();i++){

            List<String> ColsData = Rows[i].split(',');

            obj = new DataLoad__c(name=ColsData[0],city__c = ColsData[1],Country__c = ColsData[2]);

            lstD.add(obj);

        }

        system.debug('----> lstD'+lstD);

        insert lstD;

        }

        static testmethod void TestCoverage()

        {

        TestCls t=new TestCls();

        t.m1();

        }

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
SFDC_LearnerSFDC_Learner

Thanks for the reply.

But I got the below error.

Class.TestCls.m1: line 6, column 1 Class.TestCls.TestCoverage: line 25, column 1


SFDC_LearnerSFDC_Learner

System.NullPointerException: Attempt to de-reference a null object

Force.comForce.com

Add null checks before accessing any List, Variable etc. eg.

 

        if(fbody != null)

             String fContent =fbody.toString();

        List<String> Rows = fContent.split('\n');

        

        if(Rows != null && Rows.size() > 0){

           for(Integer i=0;i<Rows.size();i++){

           }

        }

 

Thanks,

Pragati