0

I had written test class for below apex class, but still it's showing 66% coverage. in the apex class public String[] LeadItems { method get method is covered, but set is not covered? How to test that method in apex test class?

Apex Class

public with sharing class CRMsettingcontroller{
    private ApexPages.StandardController sc;
    public CRM_settings__c  Objcrm{get; set;}
    public CRM_settings__c crms{get;set;}
    public Lead leadsrce{get;set;}
    public List<Lead> leadtemp = new List<Lead>();
    public Summary[] Campaigns { get; set; }
    public Summary[] Leads { get; set; }
    public Activity__c  musact;
    public boolean level1{get;set;}
    public id crmid;
    public boolean stdObjectLead {get;set;}
    public boolean stdObjectCampaign {get;set;}

    public CRMsettingcontroller() {
        This.sc = sc;
        Objcrm= new  CRM_settings__c  ();
        crms= new musqot__CRM_settings__c();
        AggregateResult[] results = [SELECT Business_region__c name 
                                     FROM CRM_data__c group by Business_region__c limit:100];
        Leads = new List<Summary>();
        for (AggregateResult ar : results) {
            Leads.add(new Summary(ar));
        }
        AggregateResult[] results2 = [SELECT  Business_area__c name 
                                      FROM CRM_data__c group by Business_Area__c limit:100];
        Campaigns = new List<Summary>()
            ;
        for (AggregateResult ar : results2) {
            Campaigns.add(new Summary(ar));
        }

        musact=[SELECT id, Use_milestones__c, Use_Performance_CRM__c 
                FROM Activity__c limit 1]; // musqot__Activity__c == API name for musqot settings 
        stdObjectLead=False;
        stdObjectCampaign=False;
        level1=false;

        if(musact.Use_Performance_CRM__c=='Salesforce (in same org)'){
            level1=true;
            //To check if Lead and Campaign object excist            
            for ( Schema.SObjectType typ : Schema.getGlobalDescribe().values()){
                String sobjSF = String.valueOf(typ);
                if (sobjSF.equals('Lead')){
                    stdObjectLead= true;
                }
                if (sobjSF.equals('Campaign')){
                    //Campaigns();
                    stdObjectCampaign= true;
                }
            }
        } 
    }

    // wrapper class to hold aggregate data
    public class Summary {
        public String Name { get; private set; }
        public Summary(AggregateResult ar) {
            Name = (String) ar.get('name');
        }
    }

    public Pagereference insertcrms() {
        if (CRM_settings__c.sObjectType.getDescribe().isCreateable())
            if(crms.Bar_color__c<>null){
                crms.Bar_color__c = crms.Bar_color__c.replaceAll(' ','');
            }
        {
            if(crms.CRM_performance_type__c!='Performance, General'){
                crms.Selector__c='';
            }
            insert crms;
        }
        String tabId = System.currentPageReference().getParameters().get('tabId');
        string pageRef='/apex/MusqotAdminPage?sfdc.tabName='+tabId+'&tab='+EncodingUtil.urlEncode('mpsett','UTF-8');
        Pagereference pg=new Pagereference (pageRef);
        return pg;
    }

    Public Pagereference cancel()
    {
        String tabId = System.currentPageReference().getParameters().get('tabId');
        string pageRef='/apex/MusqotAdminPage?sfdc.tabName='+tabId+'&tab='+EncodingUtil.urlEncode('mpsett','UTF-8');
        Pagereference pg=new Pagereference (pageRef);
        return pg;
    }

    public List<SelectOption> getMyOptions() {
        List<SelectOption> options = new List<SelectOption>(); 
        Schema.DescribeFieldResult field = CRM_Settings__c.CRM_performance_type__c.getDescribe();
        for (Schema.PicklistEntry f : field.getPicklistValues()){
            if(musact.Use_milestones__c ==false){
                if(f.getLabel()!='Milestone'){
                    options.add(new SelectOption(f.getLabel(), f.getLabel()));
                }
            }else
                options.add(new SelectOption(f.getLabel(), f.getLabel()));
        }   
        return options;     
    }

    public List<SelectOption> getwatchtype()
    {
        List<SelectOption>options = new List<SelectOption>();    
        Schema.DescribeFieldResult fieldResult =CRM_settings__c.Watch_type__c.getDescribe();
        List<Schema.PicklistEntry> crmwatch= fieldResult.getPicklistValues();

        for( Schema.PicklistEntry f:crmwatch)
        {
            options.add(new SelectOption(f.getLabel(), f.getValue()));
        }       
        return options;
    }

    public List<SelectOption> getleadsources{
        get
        { 
            if(stdObjectLead){
                //
                leadtemp=[SELECT Id, Name FROM Lead limit 250];
                Schema.DescribeFieldResult leadPickList =Lead.Leadsource.getDescribe();
                List<Schema.PicklistEntry> leadsourceList= leadPickList.getPicklistValues();
                getleadsources = new List<SelectOption>();
                for( Schema.PicklistEntry le:leadsourceList){

                    getleadsources .add(new SelectOption(le.getLabel(), le.getValue()));
                }       
                return getleadsources;
            } 
            return null;  
        }
        set;               
    } 
    public String[] LeadItems { 
        get {
            String[] selected = new List<String>();
      //    List<SelectOption> sos = this.getleadsources;
            for(SelectOption s : this.getleadsources){
                if(this.Objcrm.Expression_business_region__c!=null && this.Objcrm.Expression_business_region__c.contains(s.getValue()))
                   selected.add(s.getValue());
            }
            return selected;
        }public set {
            String selectedConcat = '';
            for(String s : value) {
                if (selectedConcat == '') 
                    selectedConcat += s;
                else selectedConcat += ';' + s;
            }
            Objcrm.Expression_business_region__c = selectedConcat;
        }
    }


    public List<SelectOption> getCampaigns{
        get
        {
            if(stdObjectCampaign){

                  return null; 
            }
            return null;    
        }
        set;               
    } 

    public String[] campItems { 
        get {
            String[] selected = new List<String>();
         //   List<SelectOption> sos = this.getCampaigns;
            for(SelectOption s : this.getCampaigns) {
                if (this.Objcrm.Expression_business_area__c!=null && this.Objcrm.Expression_business_area__c.contains(s.getValue()))
                    selected.add(s.getValue());
            }
            return selected;
        }public set {
            String selectedConcat = '';
            for(String s : value) {
                if (selectedConcat == '') 
                    selectedConcat += s;
                else selectedConcat += ';' + s;
            }
            Objcrm.Expression_business_area__c= selectedConcat;
        }
    } 
}

Apex Test Class:

@isTest(Seealldata = True)
Public class CRMSettingsEditControllertest{
    static testMethod void  CRMsettingcontroller(){ 
    musqot__CRM_settings__c mr=new musqot__CRM_settings__c();
    mr.musqot__Bar_color__c='test';
    mr.musqot__Chart__c='test';
    mr.musqot__CRM_performance_type__c='test';
    mr.musqot__Expression_business_area__c='test';
    mr.musqot__Expression_business_region__c='test';
    mr.musqot__Selector__c='test';
    insert mr;
      System.assertEquals('test', mr.musqot__Bar_color__c);
      musqot__CRM_logfile__c log=new musqot__CRM_logfile__c();
      log.musqot__Imported__c=true;
      log.musqot__Import_message__c='test';
      log.musqot__Number_of_records__c=55;
      insert log;

      CRM_data__c crm=new CRM_data__c();
      crm.musqot__Business_area__c='test';
      crm.musqot__Business_region__c='test';
      crm.musqot__CRM_import_date__c=system.today();
      crm.musqot__CRM_import_id__c='test';
      crm.musqot__CRM_logfile__c=log.id;
      crm.musqot__Date__c=system.today();
      crm.musqot__No_Deals__c=4;
      crm.musqot__No_Leads__c=4;
      crm.musqot__No_Opportunities__c=4;
     crm.musqot__Value_Deals__c=50.00;
       crm.musqot__Value_w_Opportunities__c=50.00;
       crm.musqot__Value_Opportunities__c=50.00;
       insert crm;
        musqot__Activity__c m1 =new musqot__Activity__c();
    m1.musqot__keycode__c='212';
    m1.musqot__Use_Allocation__c=true;
    m1.musqot__Use_Budget__c=true;
    m1.musqot__Use_Finance1__c=true;
    m1.musqot__Use_Forcasting__c=true;
    m1.musqot__Use_milestones__c=true;
    m1.musqot__Use_Resource__c=true;
    m1.Use_Performance_CRM__c='Salesforce (in same org)';
    System.assertEquals('212', m1.musqot__keycode__c);
    insert m1;


   test.startTest();
   PageReference pageRef = Page.CRMsettingEditpage;
   Test.setCurrentPage(pageRef); 
   ApexPages.StandardController sc = new ApexPages.standardController(crm);

   ApexPages.currentPage().getParameters().put('crmid', mr.id);      
   CRMSettingsEditController crmctrl = new CRMSettingsEditController(sc);
   List<SelectOption> watch= crmctrl.getwatchtype();
   List<SelectOption> leads=crmctrl.getleadsources;
   List<SelectOption> camps=crmctrl.getCampaigns;
   Lead temp11=crmctrl.leadsrce;
   crmctrl.Level1=true;
      try{
     crmctrl.Save();

       String[] lead1=crmctrl.LeadItems;
       String[] camp1=crmctrl.campItems;
       }catch(exception e){}
      crmctrl.cancel();
      crmctrl.getMyOptions();
     test.stopTest();
  • 1
    You have to set your variables to call their setters. i.e. crmctrl.LeadItems=some_string_array; same for campItems as well. – highfive Sep 29 '15 at 8:58
  • can u pls explain a littile bit? i can't understand... – fsales572 Sep 29 '15 at 9:05
0

In order to test the setter method, you need to actually set the value.

You are testing the getter with this line

String[] lead1=crmctrl.LeadItems;

You need to test the setter in a similar way, by actually setting the value of the property. Something like this

List<String> stringList = new List<String>{'string1','string2','string3'};
crmctrl.LeadItems = stringList;

That will give you coverage on the setter.

On another note, I would also suggest you read up up Apex testing best practices here. In particular, I this best practice

Use System.assert methods to prove that code behaves properly.

You are coding for test coverage, but you really should be using assert methods to ensure your code is behaving as you expect it to behave

1

You are using Apex Properties in your class and they are not automatic properties. So to hit / test setter of a property, you have to explicitly call the setter method and same for the getter.

Understand below is simple example from documentation. Say you have below class with a single property called prop

public class BasicProperty {
   public integer prop {
      get { return prop; }
      set { prop = value; }
   }
}

You can hit the setter and getter by,

BasicProperty bp = new BasicProperty();
bp.prop = 5;                   // Calls set accessor(setting the value) 5 will be passed in to 'value' in the setter and assigned into prop
Integer myInt = bp.prop;   // Calls get accessor(getting the value)

However if you have defined the property in your class like public integer prop{get;set;} (automatic property) you no need to bother about covering getter or setter.

Now in your case you have to set some value to property LeadItems in the test class to hit the setter.

//use necessary string array for your testing.this is just to explain the behavior
crmctrl.LeadItems = new String[]{ 'one', 'two' };
crmctrl.campItems = new String[]{ 'one', 'two' };

This will hit your setter method. Also if you are not using yet, have some assert statements in your test class to test your use cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.