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
@rajeshdulhai@rajeshdulhai 

Help for writing a test class for APEX class

Hi everyone ,

 

i want to write a test class for the following apex class . Please help me 

 

APEX class:

 

public class ImportContactsController 
{

public blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public String[] csvfilelines{get;set;}
public String[] inputvalues{get;set;}
public List<string> fieldList{get;set;}
public List<Account> sObjectList{get;set;}
public integer count =0;

 public void readcsvFile()
    { 
        csvFileLines = new String[]{}; 
        FieldList = new list<String>();
        sObjectList = new list<Account>();   
        csvAsString = csvFileBody.toString();
        csvfilelines = csvAsString.split('\n');
        inputvalues = new String[]{};
        for(string st:csvfilelines[0].split(',')) 
        FieldList.add(st);      
      
        for(Integer i=1;i<csvfilelines.size();i++)
        {
            Account obj = new Account() ;
            string[] csvRecordData = csvfilelines[i].split(',');
            obj.Name = csvRecordData[0];
            obj.Mobile__c = csvRecordData[1] ;             
            obj.Workshop_Number__c = csvRecordData[2] ;                                                 
            sObjectList.add(obj);   
        }  
                      
        Database.UpsertResult[] results = Database.upsert(sObjectList,Schema.Account.Workshop_Number__c, false);
        for (Database.UpsertResult res : results) 
        {
            if (res.isSuccess()) 
            {          
                count ++; 
            }
            else 
            {
                if (res.getErrors().size() > 0) 
                {
                     ApexPages.AddMessage( new ApexPages.Message( ApexPages.Severity.WARNING, ' ' +res.getErrors()[0].getMessage() ));
                }
            }          
        }
         ApexPages.AddMessage( new ApexPages.Message( ApexPages.Severity.INFO, +count+ ' Records Inserted ' ));         
    }
}

 

Please guide in writing a proper test class for this 

 

Thanks in advance 

 

Best Answer chosen by Admin (Salesforce Developers) 
Abhi_TripathiAbhi_Tripathi

All Answers

hitesh90hitesh90

Hi Rajesh,

 

Try to use following test class for your apex controller.

 

Test Class:

@istest
public class TestImportContactsController{
    Private Static testmethod void TestImportContactsController(){
        Account objAcc = new Account();
        objAcc.Name = 'Test Acc';
        insert objAcc;
        
        ApexPages.StandardController std = new ApexPages.StandardController(objAcc);
        ImportContactsController objImportCont = new ImportContactsController();
        objImportCont.csvFileBody = blob.valueOf('Test body\nTest New');
        objImportCont.readcsvFile();
    }
}

 

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

@rajeshdulhai@rajeshdulhai

Hi Hitesh ,

 

thanks for your reply ,

 

While using the test class you provided it is giving the following error :

 

System.ListException: List index out of bounds: 1

 

then i commented the line    //objImportCont.csvFileBody = blob.valueOf('Test body\nTest New');

 

after that its giving the following error :

 

System.NullPointerException: Argument cannot be null.

 

Here is my visualforce code associated with that controller

 

 

<apex:page controller="ImportContactsController" >
<apex:form >
    <apex:pageMessages ></apex:pageMessages>
    <apex:pageblock title="Import Accounts ">
    <apex:pageBlockSection columns="1" >
       <!-- <left> <font size="4"> Import Accounts </font> </left>
            <br/><br/>
            <table width="100%">
            <tr>
            <td align="left"> -->
            <apex:pageBlockSectionItem >
            <!--<apex:outputText value="Please select a file"></apex:outputText>
            <apex:outputLabel value="Please select a file" styleClass="p"></apex:outputLabel>
            <apex:outputText styleClass="sample" value="Please select a file :"></apex:outputText>-->
            <left><font size="2.8">Please select a file : </font> </left>           
            <apex:outputPanel layout="block">
            <apex:inputFile value="{!csvFileBody}"  filename="{!csvAsString}"/>
            <apex:commandButton value="Import to Accounts " action="{!readcsvFile}"/>
            </apex:outputPanel>
            </apex:pageBlockSectionItem>
           <!-- </td>
            </tr>
            </table>                                    
            <br/>    -->                  
     </apex:pageBlockSection>     
     <apex:pageBlockTable value="{!sObjectList}" var="loop" columns="4" width="30%">                                                                                                             
     <apex:column headerValue="Name" value="{!loop.Name}" width="5%"/>
     <apex:column headerValue="Mobile Number" value="{!loop.Mobile__c}" width="3%"/>
     <apex:column headerValue="Workshop Number" value="{!loop.Workshop_Number__c}" width="3%"/>                                                                                                                                
     </apex:pageBlockTable>
    <br/>                    
    </apex:pageblock>
     </apex:form>
</apex:page>

 

 

Please help me i am new to salesforce

 

Thanks in advance

Abhi_TripathiAbhi_Tripathi
This was selected as the best answer
hitesh90hitesh90

Hi Rajesh,

 

Try to use following test class for your apex controller i have updated it.

 

 

Test Class:

 

@istest
public class TestImportContactsController{
    Private Static testmethod void TestImportContactsController(){
        Account objAcc = new Account();
        objAcc.Name = 'Test Acc';
        insert objAcc;
        
        ApexPages.StandardController std = new ApexPages.StandardController(objAcc);
        ImportContactsController objImportCont = new ImportContactsController();
        objImportCont.csvFileBody = blob.valueOf('Test body1,Test body2, Test body3\nTest New1,Test New2, Test New3');
        objImportCont.readcsvFile();
    }
}

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

 

@rajeshdulhai@rajeshdulhai

Hi Hitesh ,

 

Your code worked , cheers

 

@istest(seeAllData=false)
public class TestTwilioImportContactsController {
    Private Static testmethod void TestTwilioImportContactsController (){
        Account objAcc = new Account();
        objAcc.Name = 'Test Acc';
        objAcc.Mobile__c ='+4791803350';
        objAcc.Workshop_Number__c ='1016';
        insert objAcc;
        
        ApexPages.StandardController std = new ApexPages.StandardController(objAcc);
        TwilioImportContactsController  objImportCont = new TwilioImportContactsController ();
        objImportCont.csvFileBody = blob.valueOf('Test body1,Test body2, Test body3\nTest New1,Test New2, Test New3');
        objImportCont.readcsvFile();
    }
}

 

i modified the 2 lines and it gave code coverage upto 93%

 

can u explain me the following code  line

objImportCont.csvFileBody = blob.valueOf('Test body1,Test body2, Test body3\nTest New1,Test New2, Test New3');

 

Thanks