+ Start a Discussion
arun rupesharun rupesh 

i need to increase code coverage for test class

Hi 

i want increase test code coverage for below class


public with sharing class CommissionEditorController {

    Map<integer,String> mapMonth = new Map<integer, String>{1=>'JAN',2=>'FEB',3=>'MAR',4=>'APR',5=>'MAY',6=>'JUN',7=>'JUL',8=>'AUG',9=>'SEP',10=>'OCT',11=>'NOV',12=>'DEC'};
    Map<String,integer> mapMonthString = new Map<String,integer>{'JAN'=>1,'FEB'=>2,'MAR'=>3,'APR'=>4,'MAY'=>5,'JUN'=>6,'JUL'=>7,'AUG'=>8,'SEP'=>9,'OCT'=>10,'NOV'=>11,'DEC'=>12};
    List<String> listMonthString = new List<String>{'APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC','JAN','FEB','MAR'};
    public Commission__c commissionRec {get;set;}
    public List<Commission__c> listCommessionRecToBeUpserted;
    public String currentFinantialYear = '';
    public Boolean showSection {get;set;}
    public Boolean showMessage {get;set;}
    public String fieldSetAPIName = '';
    public String payrollNumber = '';
    public String urlCommissionTemplate {get;set;}
    public Blob contentFile {get; set;}
    public String nameFile {get; set;}
    public List<List<String>> parsedCSV = new List<List<String>>();
    public List<Commission__c> cObjsToInsert = new List<Commission__c>();
    
    
    public CommissionEditorController(){
        showSection = false;
        showMessage = false;
        commissionRec = new Commission__c();
        Date currentDate = Date.today();
        Integer month = currentDate.Month();
        Integer year = currentDate.Year();
        commissionRec.Month__c = '' + mapMonth.get(month - 1);
        currentFinantialYear = ((month - 1)>3)?(year+' - '+((year+1)+'').subString(2)):(''+(year-1)+' - '+(year+'').subString(2));
        commissionRec.Year__c = currentFinantialYear;
        urlCommissionTemplate = System.Label.CommissionTemplateLink;
    }
    
    public void getUserTeam(){
        String userName = System.currentPageReference().getParameters().get('userId');
        User userRec = [Select Id, Name, Team__c, Payroll_Number__c from User where Name =: userName limit 1];
        payrollNumber = userRec.Payroll_Number__c;
        String fieldSetName = userRec.Team__c;
        CommissionTeamFieldSet__c fieldset = CommissionTeamFieldSet__c.getValues(fieldSetName);
        fieldSetAPIName = fieldset.FieldSet_Name__c;
        system.debug('payrollNumber ====='+payrollNumber );
        showSection = true;
        try{
            Commission__c oldRec = Database.query(getCommission(commissionRec.Month__c, commissionRec.Year__c, userRec.Id));
            if(oldRec != Null){
                System.debug('oldRec =='+ oldRec);
                commissionRec = oldRec;
            }
            else{
                commissionRec = commissionRec;
            }
        }
        Catch(Exception e){
            commissionRec = commissionRec;
        }
    }
    
    public List<Schema.FieldSetMember> getFields() {
        return getFieldSet(fieldSetAPIName, 'Commission__c');
    }

    public String getCommission(String month, String year, Id userId) {
        String query = 'SELECT ';
        for(Schema.FieldSetMember f : this.getFields()) {
            query += f.getFieldPath() + ', ';
        }
        query += 'Id, Name, Month__c, Year__c, User__c, OTC__c, Monthly_OTC__c, Cumulative_OTC__c, Weighted_Average_Performance__c, Accelerated_Decelerated_WA_Performance__c, Cumulative_Payment_Earned__c, Previous_Commission_Paid__c, Payment_Due__c, Accelerator_Bank__c FROM Commission__c';
        query += ' where Month__c =\''+month+'\' And Year__c =\''+year+'\' And User__c =\''+userId+'\' LIMIT 1';
        return query;
    }
    
    public List<Schema.FieldSetMember> getFieldSet(String fieldSetName, String ObjectName)
    {
        System.debug('fieldSetAPIName=fieldSetName==='+fieldSetName);
        Map<String, Schema.SObjectType> GlobalDescribeMap = Schema.getGlobalDescribe(); 
        Schema.SObjectType SObjectTypeObj = GlobalDescribeMap.get(ObjectName);
        Schema.DescribeSObjectResult DescribeSObjectResultObj = SObjectTypeObj.getDescribe();

        Schema.FieldSet fieldSetObj = DescribeSObjectResultObj.FieldSets.getMap().get(fieldSetName);
        System.debug('fieldSetObj.getFields()==='+fieldSetObj.getFields());
        return fieldSetObj.getFields(); 
    } 
        
    public pageReference save(){
        if(commissionRec.Id != Null){
            upsert commissionRec;
            PageReference page1 = new PageReference('/'+commissionRec.Id);
            return page1;
        }
        else{
            commissionRec.Name = payrollNumber + commissionRec.Month__c + commissionRec.Year__c;
            commissionRec.ExternalKey__c = commissionRec.Name;
            commissionRec.Payroll_Number__c = payrollNumber;
            insert commissionRec;
            PageReference page1 = new PageReference('/'+commissionRec.Id);
            return page1;
        }
        return null;
    }
    
    public pageReference reset(){
        PageReference pg = new PageReference(System.currentPageReference().getURL());
        pg.setRedirect(true);
        return pg;
    }
    
    public pageReference insertCommission(){
        listCommessionRecToBeUpserted = new List<Commission__c>();      
        Integer rowCount = 0;
        Integer colCount = 0;
        system.debug('=============contentFile-1-'+contentFile);
        if(contentFile == null){
            system.debug('=============contentFile-2-'+contentFile);
            Apexpages.addMessage( new Apexpages.message(Apexpages.Severity.ERROR, System.Label.CommissionFileNotFound)); 
        }
        else if (contentFile != null){
            system.debug('=============contentFile-3-'+contentFile);
            String fileString = contentFile.toString();
            system.debug('========fileString'+fileString);
            contentFile = null;
            parsedCSV = Commission_ParserUpload.parseCSV(fileString, false);
            rowCount = parsedCSV.size();
            for (List<String> row : parsedCSV){
                if (row.size() > colCount){
                    colCount = row.size();
                }
            }        
            cObjsToInsert = Commission_ParserUpload.csvTosObject(parsedCSV,'Commission__c') ;       
            if(cObjsToInsert.size() > 0){
                for(Commission__c coRec : cObjsToInsert){
                    Commission__c newRec = new Commission__c();
                    newRec = coRec;
                    newRec.Name = coRec.Payroll_Number__c + coRec.Month__c + coRec.Year__c;
                    newRec.ExternalKey__c = newRec.Name;
                    listCommessionRecToBeUpserted.add(newRec);
                }
            }
            if(listCommessionRecToBeUpserted.size() > 0){
                upsert listCommessionRecToBeUpserted ExternalKey__c;
                showMessage = true;
            }
        }
        return null;
    }
}



test class is---------------------
================================
@isTest(SeeAllData=true)

public class CommissionEditorControllerTest{
 
    static testMethod void saveNewTest() {
        List<Profile> lstPortalProfiles = [ Select id, usertype from Profile where name = 'System Administrator'];
        User objUser = new User( email='test-user@fakeemail.com', profileid = lstPortalProfiles[0].id, UserName='test-user@email.com', alias='tuser1', 
                                       CommunityNickName='tuser1', TimeZoneSidKey='America/New_York', LocaleSidKey='en_US', EmailEncodingKey='ISO-8859-1', LanguageLocaleKey='en_US', 
                                       FirstName = 'Test', LastName = 'User', isActive=true,Payroll_Number__c = '12345',Team__c='Direct Large SME AM');
        insert objUser;
        CommissionEditorController test1 = new CommissionEditorController();    
        test1.commissionRec.Month__c = 'Jun';
        ApexPages.currentPage().getParameters().put('userId', objUser.Name);
        //test1.getUserTeam();
        test1.save();   
    }
    
    static testMethod void saveOldTest() { 
        List<Profile> lstPortalProfiles = [ Select id, usertype from Profile where name = 'System Administrator'];
        User objUser = new User( email='test-user@fakeemail.com', profileid = lstPortalProfiles[0].id, UserName='test-user@email.com', alias='tuser1', 
                                       CommunityNickName='tuser1', TimeZoneSidKey='America/New_York', LocaleSidKey='en_US', EmailEncodingKey='ISO-8859-1', LanguageLocaleKey='en_US', 
                                       FirstName = 'Test', LastName = 'User', isActive=true,Payroll_Number__c = '12345',Team__c='Direct Large SME AM');
        insert objUser;
        CommissionEditorController test1 = new CommissionEditorController();    
        test1.commissionRec.Month__c = 'Jun';
        ApexPages.currentPage().getParameters().put('userId', objUser.Name);
        //test1.getUserTeam();
        test1.save();

        CommissionEditorController test2 = new CommissionEditorController();
        //Commented below for ChangeSet exception
        //test1.commissionRec.Month__c = 'Jun';
        test2.commissionRec.Month__c = 'Jan';
        ApexPages.currentPage().getParameters().put('userId', objUser.Name);
        //test2.getUserTeam();
        test2.save();
    }
    
    static testMethod void resetTest() {  
        PageReference pageRef = new PageReference('/CommissionEditorPage');
        Test.setCurrentPage(pageRef);
        
        CommissionEditorController test1 = new CommissionEditorController();
        test1.reset();  
    }
    
    static testMethod void uploadTest() {       
        CommissionEditorController test1 = new CommissionEditorController();
        String csvStr = 'Month__c,Payroll_Number__C,Year__c\n'+'AUG,12345,2015 - 16';
        test1.contentFile = Blob.valueOf(csvStr);  
        test1.insertCommission();
    }
    
    static testMethod void dashboardTest() {
        List<Profile> lstPortalProfiles = [ Select id, usertype from Profile where name = 'System Administrator'];
        User objUser = new User( email='test-user@fakeemail.com', profileid = lstPortalProfiles[0].id, UserName='test-user@email.com', alias='tuser1', 
                                       CommunityNickName='tuser1', TimeZoneSidKey='America/New_York', LocaleSidKey='en_US', EmailEncodingKey='ISO-8859-1', LanguageLocaleKey='en_US', 
                                       FirstName = 'Test', LastName = 'User', isActive=true,Payroll_Number__c = '12345',Team__c='Direct Large SME AM');
        insert objUser;
        
        List<User> lstUser = [SELECT Id, Name, Payroll_Number__c, Team__c FROM User where Name = :objUser.Name];
        Commission__c obj1 = new Commission__c();
        /*Map<integer,String> mapMonth = new Map<integer, String>{1=>'JAN',2=>'FEB',3=>'MAR',4=>'APR',5=>'MAY',6=>'JUN',7=>'JUL',8=>'AUG',9=>'SEP',10=>'OCT',11=>'NOV',12=>'DEC'};
        Date currentDate = Date.today();
        Integer month = currentDate.Month();
        Integer year = currentDate.Year();
        mapMonth.get(month - 1);
        String currentFinantialYear = ((month - 1)>3)?(year+' - '+((year+1)+'').subString(2)):(''+(year-1)+' - '+(year+'').subString(2));
        */

        obj1.Month__c = 'JUL';
        if(lstUser != null && lstUser.size() > 0){
            obj1.OwnerId = lstUser[0].Id;
        }
        
        String currentFinancialYear = '';
        Date currentDate = Date.today();
        string namefile='';
        Integer currentMonth = currentDate.Month();
        Integer month = 7;
        Integer year = currentDate.Year();
        if(month > currentMonth  || month <= 3){
            currentFinancialYear = ''+(year-1)+' - '+(year+'').subString(2);
        }
        else{
            currentFinancialYear = year+' - '+((year+1)+'').subString(2);
        }
        
        
        obj1.Year__c = currentFinancialYear;
        obj1.Payroll_Number__c = '12345';
        insert obj1;
        
        System.runAs(objUser){
          CommissionDashboardController test1 = new CommissionDashboardController();
            CommissionDashboardController.listCommissions();
            CommissionDashboardController.YTDCommissions('JUL');
            CommissionDashboardController.MonthCommissions('JUL');    
        }
    }
}
debradebra
I would recommend to help narrow down your request to run current test and identify which lines of code are not covered and focus your questions on uncovered lines.
arun rupesharun rupesh
Hi Debra,

below lines are not covered in my testclass code coveregre

   public void getUserTeam(){
        String userName = System.currentPageReference().getParameters().get('userId');
        User userRec = [Select Id, Name, Team__c, Payroll_Number__c from User where Name =: userName limit 1];
        payrollNumber = userRec.Payroll_Number__c;
        String fieldSetName = userRec.Team__c;
        CommissionTeamFieldSet__c fieldset = CommissionTeamFieldSet__c.getValues(fieldSetName);
        fieldSetAPIName = fieldset.FieldSet_Name__c;
        system.debug('payrollNumber ====='+payrollNumber );
        showSection = true;
        try{
            Commission__c oldRec = Database.query(getCommission(commissionRec.Month__c, commissionRec.Year__c, userRec.Id));
            if(oldRec != Null){
                System.debug('oldRec =='+ oldRec);
                commissionRec = oldRec;
            }
            else{
                commissionRec = commissionRec;
            }
        }
        Catch(Exception e){
            commissionRec = commissionRec;
        }
    }
arun rupesharun rupesh
Hi Debra,

Below are are nt covered by test class 
   public void getUserTeam(){
        String userName = System.currentPageReference().getParameters().get('userId');
        User userRec = [Select Id, Name, Team__c, Payroll_Number__c from User where Name =: userName limit 1];
        payrollNumber = userRec.Payroll_Number__c;
        String fieldSetName = userRec.Team__c;
        CommissionTeamFieldSet__c fieldset = CommissionTeamFieldSet__c.getValues(fieldSetName);
        fieldSetAPIName = fieldset.FieldSet_Name__c;
        system.debug('payrollNumber ====='+payrollNumber );
        showSection = true;
        try{
            Commission__c oldRec = Database.query(getCommission(commissionRec.Month__c, commissionRec.Year__c, userRec.Id));
            if(oldRec != Null){
                System.debug('oldRec =='+ oldRec);
                commissionRec = oldRec;
            }
            else{
                commissionRec = commissionRec;
            }
        }
        Catch(Exception e){
            commissionRec = commissionRec;
        }
    }