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
ShitalShital 

Hi guys,I need to write Test class for this code...I m very new t salesforce...any help would b appriciated

global class NewAMCLeadsForCampaign {
    
    @future (callout = true)
    public static void execute(){
        
            Http http = new Http();
            
            HttpRequest request = new HttpRequest();
            
            request.setEndpoint('http://www.startupwala.in/salseforce_automization/get_json_of_dummy_company_data.php');
            request.setMethod('POST');
            
            HttpResponse response = http.send(request);
            
            system.debug('-----response---'+response);
            
            map<String,Object> ResponseMap = (map<String,Object>)JSON.deserializeUntyped(response.getBody());
            
            system.debug('----ResponseMap-----'+ResponseMap);
            
            list<Object> objList = (list<Object>)ResponseMap.get('companies_data');
            system.debug('----objList-----'+objList);
            
            map<Integer,map<String,Object>> LeadDetailsMap = new map<Integer,map<String,Object>>();
            
            map<String,Object> InnerMap = new map<String,Object>();
            
            list<String> strList = new List<String>();
            
            list<Lead> LeadList = new list<Lead>();
            
            map<String,String> getCustomSettingMap = new map<String,String>();
                
            Integer i = 1; 
            for(Object tempOp : objList){
                
                InnerMap = (map<String,Object>)tempOp;
                LeadDetailsMap.put(i,InnerMap);
                i++;
                system.debug('----Inside LeadDetailsMap----'+LeadDetailsMap);
                system.debug('----InnerMap----'+InnerMap);
            }
            
            system.debug('----LeadDetailsMap----'+LeadDetailsMap);
            
            if(ResponseMap.get('companies_data') != null){
            
            list<Object> objectList = (list<Object>)ResponseMap.get('companies_data');
            
            Integer index = 0;
            
            for(Integer temp : LeadDetailsMap.keySet()){
                
                map<String,Object> LeadInfoMap = LeadDetailsMap.get(temp);
                
                Lead newLead = new Lead();
                
                   newLead.Company_Class__c = string.valueof(LeadInfoMap.get('company_class'));
                
                newLead.CIN__c = string.valueof(LeadInfoMap.get('corporate_identification_number'));
                
                newLead.FirstName = string.valueof(LeadInfoMap.get('first_name'));
                
                newLead.LastName = string.valueof(LeadInfoMap.get('last_name'));
                
                newLead.Company = string.valueof(LeadInfoMap.get('company_name'));
                
                newLead.Email = string.valueof(LeadInfoMap.get('email_id'));    
                
                newLead.City_of_Office__c = string.valueof(LeadInfoMap.get('city_of_office'));
                
                newLead.Company_Type__c = string.valueof(LeadInfoMap.get('company_type'));
                
                newLead.Enquiry_For__c = 'MCA Email Campaign';
                 
                 
                String str = string.valueof(LeadInfoMap.get('date_of_incorporation'));
                 
                newLead.Status = 'Open';
                string str1 = str.replace('-','/');
                 
                system.debug('---str1---'+str1);
                 
                Date dt = Date.parse(str1);
        
                system.debug('---dt---'+dt);
                
                newLead.Date_of_Incorporation__c = dt;
                
                           
                system.debug('-----newLead------'+newLead);                
                    
                LeadList.add(newLead);
                
                }
                
            system.debug('----LeadList----'+LeadList);
            
                if(LeadList.size() > 0){
                    
                    insert LeadList;
                    
                    system.debug('----LeadList----'+LeadList);
                }
            }
            
            
    }            
    
}
Manj_SFDCManj_SFDC
Hi Shital
you can refer this https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm
Waqar Hussain SFWaqar Hussain SF
@future (callout = true)
    public static void execute(){
        
            Http http = new Http();
            
            HttpRequest request = new HttpRequest();
            
            request.setEndpoint('http://www.startupwala.in/salseforce_automization/get_json_of_dummy_company_data.php');
            request.setMethod('POST');
            
            HttpResponse response;
			if(!test.isRunningTest()){
				response = http.send(request);
			}
            
            system.debug('-----response---'+response);
            
			map<String,Object> ResponseMap = new map<String,Object>();
			if(test.isRunningTest()){
				//create fake response here, copy and paste the original response in below variable which you are getting in line 16.
				string respBody = //paste fake response json hare
				ResponseMap = (map<String,Object>)JSON.deserializeUntyped(respBody);
			}else{
            ResponseMap = (map<String,Object>)JSON.deserializeUntyped(response.getBody());
            }
            system.debug('----ResponseMap-----'+ResponseMap);
            
            list<Object> objList = (list<Object>)ResponseMap.get('companies_data');
            system.debug('----objList-----'+objList);
            
            map<Integer,map<String,Object>> LeadDetailsMap = new map<Integer,map<String,Object>>();
            
            map<String,Object> InnerMap = new map<String,Object>();
            
            list<String> strList = new List<String>();
            
            list<Lead> LeadList = new list<Lead>();
            
            map<String,String> getCustomSettingMap = new map<String,String>();
                
            Integer i = 1; 
            for(Object tempOp : objList){
                
                InnerMap = (map<String,Object>)tempOp;
                LeadDetailsMap.put(i,InnerMap);
                i++;
                system.debug('----Inside LeadDetailsMap----'+LeadDetailsMap);
                system.debug('----InnerMap----'+InnerMap);
            }
            
            system.debug('----LeadDetailsMap----'+LeadDetailsMap);
            
            if(ResponseMap.get('companies_data') != null){
            
            list<Object> objectList = (list<Object>)ResponseMap.get('companies_data');
            
            Integer index = 0;
            
            for(Integer temp : LeadDetailsMap.keySet()){
                
                map<String,Object> LeadInfoMap = LeadDetailsMap.get(temp);
                
                Lead newLead = new Lead();
                
                   newLead.Company_Class__c = string.valueof(LeadInfoMap.get('company_class'));
                
                newLead.CIN__c = string.valueof(LeadInfoMap.get('corporate_identification_number'));
                
                newLead.FirstName = string.valueof(LeadInfoMap.get('first_name'));
                
                newLead.LastName = string.valueof(LeadInfoMap.get('last_name'));
                
                newLead.Company = string.valueof(LeadInfoMap.get('company_name'));
                
                newLead.Email = string.valueof(LeadInfoMap.get('email_id'));    
                
                newLead.City_of_Office__c = string.valueof(LeadInfoMap.get('city_of_office'));
                
                newLead.Company_Type__c = string.valueof(LeadInfoMap.get('company_type'));
                
                newLead.Enquiry_For__c = 'MCA Email Campaign';
                 
                 
                String str = string.valueof(LeadInfoMap.get('date_of_incorporation'));
                 
                newLead.Status = 'Open';
                string str1 = str.replace('-','/');
                 
                system.debug('---str1---'+str1);
                 
                Date dt = Date.parse(str1);
        
                system.debug('---dt---'+dt);
                
                newLead.Date_of_Incorporation__c = dt;
                
                           
                system.debug('-----newLead------'+newLead);                
                    
                LeadList.add(newLead);
                
                }
                
            system.debug('----LeadList----'+LeadList);
            
                if(LeadList.size() > 0){
                    
                    insert LeadList;
                    
                    system.debug('----LeadList----'+LeadList);
                }
            }
            
            
    }

 
Maharajan CMaharajan C
Hi Shital,

Please find the updated Future Method and Test Class by using that surely you can cover more than 95%.

And i also i tested the below class it working fine!!!


Apex Class:

global class NewAMCLeadsForCampaign {
    
    @future (callout = true)
    public static void execute(){
        
            Http http = new Http();
            
            HttpRequest request = new HttpRequest();
            
            request.setEndpoint('http://www.startupwala.in/salseforce_automization/get_json_of_dummy_company_data.php');
            request.setMethod('POST');
            HttpResponse response = new HttpResponse();
            map<String,Object> ResponseMap = new map<String,Object>();
            if(!test.isRunningTest())
            {
            response = http.send(request);
            system.debug('-----response---'+response);
            ResponseMap = (map<String,Object>)JSON.deserializeUntyped(response.getBody());
            }
            else
            {
                //create fake response here, copy and paste the original response in below variable which you are getting in line 16.
                string respBody =  '{ "companies_data": [ { "first_name": "Shreenath", "last_name": "Jamdade", "company_name": "Shreenath Coders Private Limited", "corporate_identification_number": "UN1234567890", "email_id": "shreenath.jamdade@startupwala.com", "city_of_office": "Pune", "date_of_incorporation": "1/2/2018", "company_class": "23", "company_type": "OPC" }, { "first_name": "Richa", "last_name": "Sethi", "company_name": "Marketing Kida Private Limited", "corporate_identification_number": "UN12345540", "email_id": "richa.sethi@startupwala.com", "city_of_office": "Pune", "date_of_incorporation": "2/2/2018", "company_class": "25", "company_type": "PVT" } ] }';
                ResponseMap = (map<String,Object>)JSON.deserializeUntyped(respBody);
            }
                        
                        
            system.debug('----ResponseMap-----'+ResponseMap);
            
            list<Object> objList = (list<Object>)ResponseMap.get('companies_data');
            system.debug('----objList-----'+objList);
            
            map<Integer,map<String,Object>> LeadDetailsMap = new map<Integer,map<String,Object>>();
            
            map<String,Object> InnerMap = new map<String,Object>();
            
            list<String> strList = new List<String>();
            
            list<Lead> LeadList = new list<Lead>();
            
            map<String,String> getCustomSettingMap = new map<String,String>();
                
            Integer i = 1; 
            for(Object tempOp : objList){
                
                InnerMap = (map<String,Object>)tempOp;
                LeadDetailsMap.put(i,InnerMap);
                i++;
                system.debug('----Inside LeadDetailsMap----'+LeadDetailsMap);
                system.debug('----InnerMap----'+InnerMap);
            }
            
            system.debug('----LeadDetailsMap----'+LeadDetailsMap);
            
            if(ResponseMap.get('companies_data') != null){
            
            list<Object> objectList = (list<Object>)ResponseMap.get('companies_data');
            
            Integer index = 0;
            
            for(Integer temp : LeadDetailsMap.keySet()){
                
                map<String,Object> LeadInfoMap = LeadDetailsMap.get(temp);
                
                Lead newLead = new Lead();
                
                newLead.Company_Class__c = string.valueof(LeadInfoMap.get('company_class'));
                
                newLead.CIN__c = string.valueof(LeadInfoMap.get('corporate_identification_number'));
                
                newLead.FirstName = string.valueof(LeadInfoMap.get('first_name'));
                
                newLead.LastName = string.valueof(LeadInfoMap.get('last_name'));
                
                newLead.Company = string.valueof(LeadInfoMap.get('company_name'));
                
                newLead.Email = string.valueof(LeadInfoMap.get('email_id'));    
                
                newLead.City_of_Office__c = string.valueof(LeadInfoMap.get('city_of_office'));
                
                newLead.Company_Type__c = string.valueof(LeadInfoMap.get('company_type'));
                
                newLead.Enquiry_For__c = 'MCA Email Campaign';
                 
                 
                String str = string.valueof(LeadInfoMap.get('date_of_incorporation'));
                 
                newLead.Status = 'Open';
                string str1 = str.replace('-','/');
                 
                system.debug('---str1---'+str1);
                 
                Date dt = Date.parse(str1);
        
                system.debug('---dt---'+dt);
                
                newLead.Date_of_Incorporation__c = dt;
                
                           
                system.debug('-----newLead------'+newLead);                
                    
                LeadList.add(newLead);
                
                }
                
            system.debug('----LeadList----'+LeadList);
            
                if(LeadList.size() > 0){
                    
                    insert LeadList;
                    
                    system.debug('----LeadList----'+LeadList);
                }
            }
            
            
    }            
}


Test Class:

@isTest
Public class NewAMCLeadsForCampaign_Test{

@isTest static void test1() {
{
    User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
       // System.runAs() allows mixed DML operations in test context
        System.runAs(thisUser) {
            // startTest/stopTest block to run future method synchronously
            Test.startTest();        
            NewAMCLeadsForCampaign.execute();
            Test.stopTest();
        }
        
}
}
}



And one more suggestiom from my side the date_of_incorporation format is like 22/2/2018 so you have to check this format because Salesforce Accept 2/22/2018 format i think so please handle Date Parsing properly from String. 


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj