+ Start a Discussion
SFDC Apex DevSFDC Apex Dev 

Can anyone help me to write the test class to cover the below code...

APEX CLASS
public class LeadTerritoryManagementController {

    @AuraEnabled
    public static wrapperClass getLeadAssignmentTerritories(){
        wrapperClass returnwrapperClass = new  wrapperClass();
        returnwrapperClass.leadAssignmentSAE = [SELECT Name, Assigned_SAE__r.Name, SAE_Territory_Name__c,Assigned_SAE__c FROM SAE_Assignment_table__c];
        returnwrapperClass.leadAssignmentSDR = [SELECT Name, AE_Territory_Name__c, Assigned_AE__r.Name, Assigned_SDR__r.Name FROM AE_Assignment_table__c];
        return returnwrapperClass;
    }
    // method for fetch User values dynamic
    @AuraEnabled
    public static List<User> searchUser() {
        List<User> activeUsers = [SELECT Id,Name,ProfileId,IsActive FROM User WHERE isActive =true LIMIT 10];
        return activeUsers;
    }
    // method for update records after inline editing
    @AuraEnabled
    public static wrapperClass saveleadAssignmentSDR(string wrapperClassDetail) {
        wrapperClass wrapperClassData= (wrapperClass)System.JSON.deserializeStrict(wrapperClassDetail, wrapperClass.class);
        List<AE_Assignment_table__c> leadAssignmentAEList=new List<AE_Assignment_table__c>();
        leadAssignmentAEList=wrapperClassData.leadAssignmentSDR;
        update leadAssignmentAEList;

        List<SAE_Assignment_table__c> leadAssignmentSAEList=new List<SAE_Assignment_table__c>();
        leadAssignmentSAEList=wrapperClassData.leadAssignmentSAE;
        update leadAssignmentSAEList;

        return wrapperClassData;
    }

    public class wrapperClass{
        @AuraEnabled public List<SAE_Assignment_table__c> leadAssignmentSAE{get;set;}
        @AuraEnabled public List<AE_Assignment_table__c> leadAssignmentSDR{get;set;}
    }
}

TEST CLASS
@isTest
public class LeadTerritoryManagementController_Test {
    
    static testmethod void controllerTest(){
        User usr = [SELECT Id,Name,ProfileId,IsActive FROM User WHERE isActive =true LIMIT 1];
        
        System.runAs(usr){
            Test.startTest();
            List<AE_Assignment_table__c> aeTable = new List<AE_Assignment_table__c>();
            AE_Assignment_table__c AE = new AE_Assignment_table__c();
            AE.AE_Territory_Name__c = 'QWERTY';
            AE.Assigned_AE__c = usr.Id;
            AE.Assigned_SDR__c = usr.Id;
            aeTable.add(AE);
            insert aeTable;
            
            List<SAE_Assignment_table__c> saeTable = new List<SAE_Assignment_table__c>();
            SAE_Assignment_table__c SAE = new SAE_Assignment_table__c();
            SAE.Assigned_SAE__c = usr.Id;
            SAE.SAE_Territory_Name__c = 'ABC';
            saeTable.add(SAE);
            insert saeTable;
            
            LeadTerritoryManagementController.getLeadAssignmentTerritories();
            LeadTerritoryManagementController.searchUser();
            Test.stopTest();
        }        
    }   
}

only 52% code coverage... 3rd method the bold one not covered...
Best Answer chosen by SFDC Apex Dev
Raj VakatiRaj Vakati
Try this
 
@isTest
public class LeadTerritoryManagementController_Test {
    
    static testmethod void controllerTest(){
        User usr = [SELECT Id,Name,ProfileId,IsActive FROM User WHERE isActive =true LIMIT 1];
        
        System.runAs(usr){
            Test.startTest();
            List<AE_Assignment_table__c> aeTable = new List<AE_Assignment_table__c>();
            AE_Assignment_table__c AE = new AE_Assignment_table__c();
            AE.AE_Territory_Name__c = 'QWERTY';
            AE.Assigned_AE__c = usr.Id;
            AE.Assigned_SDR__c = usr.Id;
            aeTable.add(AE);
            insert aeTable;
            
            List<SAE_Assignment_table__c> saeTable = new List<SAE_Assignment_table__c>();
            SAE_Assignment_table__c SAE = new SAE_Assignment_table__c();
            SAE.Assigned_SAE__c = usr.Id;
            SAE.SAE_Territory_Name__c = 'ABC';
            saeTable.add(SAE);
            insert saeTable;
            
			LeadTerritoryManagementController.wrapperClass wmmmm = new LeadTerritoryManagementController.wrapperClass();
			wmmmm.leadAssignmentSAE =saeTable; 
			wmmmm.leadAssignmentSDR = aeTable ;
			String val =JSON.serialize(wmmmm); 
			
            LeadTerritoryManagementController.getLeadAssignmentTerritories();
            LeadTerritoryManagementController.searchUser();
			LeadTerritoryManagementController.saveleadAssignmentSDR(val);
            Test.stopTest();
        }        
    }   
}