+ Start a Discussion
NNRNNR 

test class uregent requirement

public PageReference exportData()
    {
        String ssql;
        pageReference pg = null;
        String header = setFieldHeader(true);      
            
        if(!String.isEmpty(header))          
        {               
            fieldHeader = header;
            if(exportEncodingType == 'Unicode') {
                fieldHeader = EncodingUtil.convertFromHex('EFBBBF').toString() + fieldHeader;
            }
            system.debug('???selectedObjectName'+selectedObjectName);
            if (selectedObjectName == 'GSD_Resource_Skill__c') {
                fieldList.add('Resource__r.CKSW__ID__c');
                if (CurrentList != null && CurrentList.size() > 0 && filterOption == 'country') {
                    ssql = 'Select ' + String.join(fieldList, ',') + ' FROM GSD_Resource_Skill__c WHERE Resource__r.CKSW__COUNTRY__c In (\'' + String.join(CurrentList, '\',\'') + '\') LIMIT 50000';
                } else if (filterOption == 'district') {
                    ssql = 'Select ' + String.join(fieldList, ',') + ' FROM GSD_Resource_Skill__c WHERE Resource__r.CKSW__District__c In (\'' + String.join(CurrentList, '\',\'') + '\') LIMIT 50000';
                } else {
                    ssql = 'Select ' + String.join(fieldList, ',') + ' FROM GSD_Resource_Skill__c LIMIT 50000';
                }
            } else if (selectedObjectName == 'CKSW__Engineer_Equipment__c') {                
                if (CurrentList != null && CurrentList.size() > 0 && filterOption == 'country') {
                    ssql = 'Select ' + String.join(fieldList, ',') + ' FROM CKSW__Engineer_Equipment__c WHERE CKSW__COUNTRY__c In (\'' + String.join(CurrentList, '\',\'') + '\') LIMIT 50000';
                } else if (filterOption == 'district') {
                    ssql = 'Select ' + String.join(fieldList, ',') + ' FROM CKSW__Engineer_Equipment__c WHERE CKSW__District__c In (\'' + String.join(CurrentList, '\',\'') + '\') LIMIT 50000';
                } else {
                    ssql = 'Select ' + String.join(fieldList, ',') + ' FROM CKSW__Engineer_Equipment__c LIMIT 50000';
                }
            }  else if (selectedObjectName == 'GSD_Resource_District__c') {
                fieldList.add('Resource__r.CKSW__ID__c');                
                if (CurrentList != null && CurrentList.size() > 0 && filterOption == 'country') {
                    ssql = 'Select ' + String.join(fieldList, ',') + ' FROM GSD_Resource_District__c WHERE Resource__r.CKSW__COUNTRY__c In (\'' + String.join(CurrentList, '\',\'') + '\') LIMIT 50000';
                } else if (filterOption == 'district') {
                    ssql = 'Select ' + String.join(fieldList, ',') + ' FROM GSD_Resource_District__c WHERE Resource__r.CKSW__District__c In (\'' + String.join(CurrentList, '\',\'') + '\') LIMIT 50000';
                } else {
                    ssql = 'Select ' + String.join(fieldList, ',') + ' FROM GSD_Resource_District__c LIMIT 50000';
                }                               
            } else if (selectedObjectName == 'GSD_Resource_District_Mapping__c') {
               // fieldList.add('GSD_Resource__r.First_name__c');
               // fieldList.add('GSD_Resource__r.Last_name__c');
                
                Integer index1 = 0;
                for(String strList : fieldList) {
                    if(strList == 'GSD_Resource__r.Name') fieldList[index1] = 'GSD_Resource__r.User__r.Name';                 
                    index1++;
                }              
                fieldList.add('GSD_Resource__r.Employee_ID__c');               
                if (CurrentList != null && CurrentList.size() > 0 && filterOption == 'country') {
                    ssql = 'Select ' + String.join(fieldList, ',') + ' FROM GSD_Resource_District_Mapping__c WHERE GSD_Resource__r.Country_ISO__r.name In (\'' + String.join(CurrentList, '\',\'') + '\') LIMIT 50000';
                } else if (filterOption == 'district') {
                    ssql = 'Select ' + String.join(fieldList, ',') + ' FROM GSD_Resource_District_Mapping__c WHERE GSD_Resource__r.Primary_District__c In (\'' + String.join(CurrentList, '\',\'') + '\') LIMIT 50000';
                } else {
                    ssql = 'Select ' + String.join(fieldList, ',') + ' FROM GSD_Resource_District_Mapping__c LIMIT 50000';
                }
            } else if (selectedObjectName == 'GSD_Resource_Skills_mapping__c') {         
                Integer index = 0;
                for(String strList : fieldList) {
                    if(strList == 'Resource_ID__r.Name') fieldList[index] = 'Resource_ID__r.User__r.Name';                 
                    index++;
                }                               
                fieldList.add('Resource_ID__r.employee_id__c');                 
                if (filterOption == 'country') {
                    ssql = 'Select ' + String.join(fieldList, ',') + ' FROM GSD_Resource_Skills_mapping__c WHERE Resource_ID__r.Country_ISO__r.name In (\'' + String.join(CurrentList, '\',\'') + '\')  AND Web_App_Employee_Id__c = \''+'\' LIMIT 50000';
                } else if (filterOption == 'district') {
                    ssql = 'Select ' + String.join(fieldList, ',') + ' FROM GSD_Resource_Skills_mapping__c WHERE Resource_ID__r.Primary_District__c In (\'' + String.join(CurrentList, '\',\'') + '\') AND Web_App_Employee_Id__c = \''+'\' LIMIT 50000';
                }
                else {                                
                    ssql = 'Select ' + String.join(fieldList, ',') + ' FROM GSD_Resource_Skills_mapping__c LIMIT 50000';              
                }                           
            } else if (selectedObjectName == 'GSD_Resource__c') {
                List<String> templist = new List<String>();             
                templist.add(fieldlist[0]);             
                templist.add('User__r.Legacy_Employee_Ref__c');
                fieldList[1] = 'User__r.Name';
                fieldlist.remove(0);
                templist.addAll(fieldlist);
                fieldlist = templist;
                /*Integer index = 0;
                for(String strList : fieldList) {
                    if(strList == 'Employee_ID__c') fieldList[index] = 'User__r.Name';                 
                    index++;
                }           
                fieldList.add('User__r.Legacy_Employee_Ref__c');*/
                if (filterOption == 'country') {
                    ssql = 'Select ' + String.join(fieldList, ',') + ' FROM GSD_Resource__c WHERE Country_ISO__r.Name In (\'' + String.join(CurrentList, '\',\'') + '\') LIMIT 50000';
                } else if (filterOption == 'district') {
                    ssql = 'Select ' + String.join(fieldList, ',') + ' FROM GSD_Resource__c WHERE Primary_District__c In (\'' + String.join(CurrentList, '\',\'') + '\') LIMIT 50000';
                } else {
                    ssql = 'Select ' + String.join(fieldList, ',') + ' FROM GSD_Resource__c LIMIT 50000';                           
                }
            }
            system.debug('??? fieldList......'+fieldList);
            system.debug('??? query structure...'+ssql);
            
            objLst = new List < sObject > ();
            ListobjLst = new List <List<sObject>> ();  
            for (sobject s: Database.Query(ssql)) {
                   objLst.add(s);
                if (objLst.size() == 1000) { //limit of vf repeat components
                    ListobjLst.add(objLst);
                    objLst = new List < sobject > ();
                }
            }
            ListobjLst.add(objLst);
            pg = new pageReference('/apex/GSDBulkTemplate');
            pg.setRedirect(false);
        }        
        return pg;
    }
Abhi_TripathiAbhi_Tripathi
Hi There, 

If you are having issue in passing the test class then please do post that , other wise for test class learnig go for this post it will definetly help you
http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html

Regards,
Abhi Tripathi
Salesforce Certified Developer
NNRNNR
HI Abhi
@isTest
        public Class GSDBulkExportImportTest
        {                       
            static CKSW__Engineer_Equipment__c resourceDataToInsert;  
            static GSD_Resource__c gsdnewresourceDataToInsert;
            static List<CKSW__Region__c> regionList;
            static List<CKSW__District__c> districtList;
            static List<World_Region__c> WorldRegionList;
                        
            //Check for CustomMap
            static testMethod void customMapTest()
            {
                GSDCustomMap customMapObj = new GSDCustomMap();
                customMapObj.get('123');
                customMapObj.put('123','abc');
                customMapObj.getCustomMap();
            }
            
            //Check for Positive Test
            static testMethod void uploadedSkillTest()
            {
                insertCustomSetting();                
                Test.setCurrentPage(Page.GSDBulkExportImport);               
                GSDBulkExportImport obj = new GSDBulkExportImport();           
                obj.showImportExport();
                System.assertEquals(null,obj.getuploadedSkills());                             
            }                   
            
            static testMethod void resourceSkillImportTest() {                
                insertCustomSetting();
                try{
                insertDataIntoResourceSkill();}
                Catch(Exception e){}
                Test.setCurrentPage(Page.GSDBulkExportImport);
               
                List<String> initList = new List<String>();
                GSDCustomMap initialCountryMap = new GSDCustomMap();
                initialCountryMap.put('US','US');
                initList.add('US');
                //  added by kiran
                World_Region__c obj= new World_Region__c();
                obj.name='india';
                World_Region__c obj1= new World_Region__c();
                obj.name='india';
                World_Region__c obj2= new World_Region__c();
                obj.name='india';
                WorldRegionList.add(obj);
                WorldRegionList.add(obj1);
                WorldRegionList.add(obj2);
                insert WorldRegionList;// end of code
                GSDBulkExportImport obj = new GSDBulkExportImport();
                obj.bulkOption = 'import';
                PageReference template = obj.downloadTemplate();
                obj.selectedObjectName = 'GSD_Resource_Skill__c';          
                 
                obj.checkBox=true;                                
                obj.contentFile = Blob.valueOf(generateImportData(obj.selectedObjectName,10,null,',','U'));
                test.startTest();                                       
                    PageReference resourcePageRef = obj.ReadFile();
                    template = obj.downloadTemplate();                  
                test.stopTest();                
                System.assertEquals(null, resourcePageRef);
                }
                
                //start RDPM
                
                static testMethod void resourceSkillExportTest() {                
                insertCustomSetting();
                try{
                insertDataIntoResourceSkill();}
                Catch(Exception e){}
                Test.setCurrentPage(Page.GSDBulkExportImport);
               
               // List<String> initList = new List<String>();
               // GSDCustomMap initialCountryMap = new GSDCustomMap();
                //initialCountryMap.put('US','US');
               // initList.add('US');
                
                GSDBulkExportImport obj = new GSDBulkExportImport();
                obj.bulkOption = 'export';
                PageReference template = obj.downloadTemplate();
                obj.selectedObjectName = 'GSD_Resource_Skill__c';                                        
                obj.checkBox=true;                                
                obj.contentFile = Blob.valueOf(generateImportData(obj.selectedObjectName,10,null,',','U'));
                test.startTest();                                       
                    PageReference resourcePageRef = obj.ExportData();
                    template = obj.downloadTemplate();                  
                test.stopTest();                
                //System.assertEquals(null, resourcePageRef);
                
                }
                
                // end RDPM
            
            //start RDPM
            
            static testMethod void GSDResourceSkillsMappingImportTest() {                
                insertCustomSetting();
                try{
                insertDataIntoGSDResourceSkillsMapping();}
                Catch(Exception e){}
                Test.setCurrentPage(Page.GSDBulkExportImport);
               
                List<String> initList = new List<String>();
                GSDCustomMap initialCountryMap = new GSDCustomMap();
                initialCountryMap.put('US','US');
                initList.add('US');
                
                GSDBulkExportImport obj = new GSDBulkExportImport();
                obj.bulkOption = 'import';
                PageReference template = obj.downloadTemplate();
                obj.selectedObjectName = 'GSD_Resource_Skills_Mapping__c';                                        
                obj.checkBox=true;                                
                obj.contentFile = Blob.valueOf(generateImportData(obj.selectedObjectName,10,null,',','U'));
                test.startTest();                                       
                    PageReference resourcePageRef = obj.ReadFile();
                    template = obj.downloadTemplate();                  
                test.stopTest();                
                System.assertEquals(null, resourcePageRef);
            }
            
          
            //End RDPM
            
            // start RDPM
            
            static testMethod void GSDResourceDistrictImportTest() {                
                insertCustomSetting();
                try{
                insertDataIntoResourceDistrict();}
                Catch(Exception e){}
                Test.setCurrentPage(Page.GSDBulkExportImport);
               
                List<String> initList = new List<String>();
                GSDCustomMap initialCountryMap = new GSDCustomMap();
                initialCountryMap.put('US','US');
                initList.add('US');
                
                GSDBulkExportImport obj = new GSDBulkExportImport();
                obj.bulkOption = 'import';
                PageReference template = obj.downloadTemplate();
                obj.selectedObjectName = 'GSD_Resource_District__c';                                        
                obj.checkBox=true;                                
                obj.contentFile = Blob.valueOf(generateImportData(obj.selectedObjectName,10,null,',','U'));
                test.startTest();                                       
                    PageReference resourcePageRef = obj.ReadFile();
                    template = obj.downloadTemplate();                  
                test.stopTest();                
                System.assertEquals(null, resourcePageRef);
            }
            
            // End RDPM
            
            // start RDPM
            
            static testMethod void GSDResourceDistrictMappingImportTest() {                
                insertCustomSetting();
                try{
                insertDataIntoGSDResourceDistrictMapping();}
                Catch(Exception e){}
                Test.setCurrentPage(Page.GSDBulkExportImport);
               
                List<String> initList = new List<String>();
                GSDCustomMap initialCountryMap = new GSDCustomMap();
                initialCountryMap.put('US','US');
                initList.add('US');
                
                GSDBulkExportImport obj = new GSDBulkExportImport();
                obj.bulkOption = 'import';
                PageReference template = obj.downloadTemplate();
                obj.selectedObjectName = 'GSD_Resource_District_Mapping__c';                                        
                obj.checkBox=true;                                
                obj.contentFile = Blob.valueOf(generateImportData(obj.selectedObjectName,10,null,',','U'));
                test.startTest();                                       
                    PageReference resourcePageRef = obj.ReadFile();
                    template = obj.downloadTemplate();                  
                test.stopTest();                
                System.assertEquals(null, resourcePageRef);
            }
            
            // End RDPM
            
            static testMethod void resourceSkillInsertImportTest() {                
                insertCustomSetting();
                try{
                insertDataIntoResourceSkill();}
                Catch(Exception e){}
                Test.setCurrentPage(Page.GSDBulkExportImport);
                GSDBulkExportImport obj = new GSDBulkExportImport();                
                obj.selectedObjectName = 'GSD_Resource_Skill__c';
                obj.bulkOption = 'import';                          
                obj.checkBox=true;                                
                obj.contentFile = Blob.valueOf(generateImportData(obj.selectedObjectName,10,null,',','I'));
                test.startTest();
                    PageReference resourcePageRef = obj.ReadFile();             
                    obj.bulkOption = 'export';                  
                    PageReference resourceExportPageRef = obj.exportData();             
                    resourceExportPageRef = obj.exportData();
                test.stopTest();                
                System.assert(obj.ListobjLst.size() > 0);
            }
            
          and how to call the batch apex from test class
veera kiranveera kiran
Hi Abhi/NNR,

I am to having same issue can you guide me how to get success with this issue, how to call excute method in test class


    
            Id BatchInstanceId = Database.executeBatch(new GSDBulkExportImportBatchApex(Blob.valueOf(cvsFileLines), selectedObjectName, checkBox), 200);
            ApexPages.Message infomsg = new ApexPages.Message(ApexPages.severity.CONFIRM, Label.GSD_BulkUpload_Success_Messages1);
            ApexPages.addMessage(infomsg);
            PageReference newocp = new PageReference('/apex/GSDBulkExportImport');
            newocp.setRedirect(true);            
            return null;
        }        

Thanks,
Kiran
David ZhuDavid Zhu
The following gives your example of how to test a batch Apex.
1. Define implement a batch apex, called "Mybatch".
global class mybatch implements Database.Batchable<sObject> {
............
       global Database.QueryLocator start(Database.BatchableContext BC){
            return Database.getQueryLocator(query);
        }
        global void execute(Database.BatchableContext BC, List<sObject> scope){
                delete scope;
        }
        global void finish(Database.BatchableContext BC){}
}

2.Write the test class:
public static testMethod void testBatch() {
           //like normal test case, build test data. 

           Test.StartTest();

           MyBatch mb = new MyBatch();

           mb.query='SELECT ... From .... where .... LIMIT 200';

           ID batchprocessid = Database.executeBatch(mb);

           Test.StopTest();  //Batch will run right after StopTest() in test method

           System.AssertEquals(expecteddata, mb.result);  //if inserting bulk data to object, you can also check the records number in the object, and etc.
}
veera kiranveera kiran
Hi David,
This code is not working as it gives error.

As  execution is in controller class and i am calling from batch class, how to call the execution from controller so it covers the batch class also i am getting problem how to call the  Excute method in test Class

Thanks,
Veera Kiran