7

I am new to Salesforce. I have written a Apex class, where I've used an inner class ( PageSection) and I am creating a list of that innerclass and using in VF as a merge variable.

My question is how can I cover the inner class in Test Method ?

 public with sharing class ViewRelatedController {

    public Account acct {get; set;}
    public List<PageSection> sectionList { get; private set; } // merge variable

    public DummyObject__c dummyObj{get; set;} // getting datefield from custom object 

    public Decimal interactionMonth {get;private set;}
    public Decimal maxList {get;private set;}
    public Decimal maxTotal {get;private set;}
    Map<String, PageSection> emailCategoryMap;
    Map<String, PageSection> caseCategoryMap;    



   // Inner class

    private class PageSection {
        public String name {get; private set;} 

        // Only one of the following to members will be populated
        public List<String> caseCategoryList {get; private set;} 
        public List<String> emailCategoryList {get; private set;} 

        // Only one of the following to members will be populated
        public List<Case>caseList {get; private set;} 
        public List<RepositoryEmail__c> emailList {get; private set;} 

        public Boolean caseCategoryList() { 
          return caseCategoryList != null; 
        }

        public Boolean hasCaseCategoryList {
            get {
                return caseCategoryList != null && !caseCategoryList.isEmpty();
            }
            set {}
        }

        public Boolean emailCategoryList() { 
          return emailCategoryList != null; 
        }

        public Boolean hasEmailCategoryList {
            get {
                return emailCategoryList != null && !emailCategoryList.isEmpty();
            }
            set {}
        }

        public Boolean getHasData() {
          return  (caseList != null && !caseList.isEmpty())|| ( emailList != null && !emailList.isEmpty());
        }

        public Boolean hasGetHasData {
            get {
                return  (caseList != null && !caseList.isEmpty())|| ( emailList != null && !emailList.isEmpty());
            }
            set {}
        }

    }


    //Controller

    public ViewRelatedController(){ 
        accountId = ApexPages.currentPage().getParameters().get('id');                
        acct = fetchAccount(accountId);

        dummyObj = new DummyObject__c();
        dummyObj.EndDateA__c = System.today();


        //Fetching the instance of  "My_Constants__c" custom setting.
        My_Constants__c appConstantSettings = My_Constants__c.getInstance();

        if(appConstantSettings != null && appConstantSettings.ViewRelatedCEInteractionMonths__c != null){
             interactionMonth = appConstantSettings.ViewRelatedCEInteractionMonths__c;
        }

        if(interactionMonth != null && (interactionMonth - interactionMonth.round(RoundingMode.DOWN))== 0.0){
            dummyObj.StartDateA__c = date.today().addMonths(-1*(interactionMonth.intValue()));
        }else if(appConstantSettings.ViewRelatedCEInteractionMonths__c == null){           
            dummyObj.StartDateA__c = date.today().addMonths(-1);
        }else{         
            dummyObj.StartDateA__c = date.today().addDays(-1*(interactionMonth*30).intValue());
        }

        if(appConstantSettings != null && appConstantSettings.ViewRelatedCEInteractionMaxList__c != null){
            maxList = (appConstantSettings.ViewRelatedCEInteractionMaxList__c).intValue();
        }else{
            maxList = 100;
        }

        if(appConstantSettings != null && appConstantSettings.ViewRelatedCEInteractionMaxTotal__c != null){
            maxTotal = (appConstantSettings.ViewRelatedCEInteractionMaxTotal__c).intValue();
        }else{
            maxTotal = 800;
        }

        //Instantiate sectionList   
        sectionList = new List<PageSection>();           

        emailCategoryMap = new Map<String, PageSection>();
        caseCategoryMap = new Map<String, PageSection>();

        for(MyConfig__c custObj : 
                                                    [
                                                       SELECT Name,Case_Category__c,Category_Type__c,Email_Category__c 
                                                       FROM MyConfig__c
                                                       ORDER BY Sort_Order__c
                                                    ]){


            PageSection psec = new PageSection();
            psec.name = custObj.Name;


            psec.emailCategoryList = new List<String>();
            if(custObj.Category_Type__c == 'Email' && custObj.Email_Category__c != null) {
                psec.emailCategoryList = custObj.Email_Category__c.split(';');
            }        
            emailCategorySet.addAll(psec.emailCategoryList);    

            psec.caseCategoryList  = new List<String>();
            if(custObj.Category_Type__c == 'Case' && custObj.Case_Category__c != null) {
                psec.caseCategoryList = custObj.Case_Category__c.split(';');
            }
            caseCategorySet.addAll(psec.caseCategoryList);

            psec.caseList = new List<Case>();         
            psec.emailList = new List<RepositoryEmail__c>();            

            if(!psec.emailCategoryList.isEmpty() && psec.emailCategoryList!= null){
                for(String emailCategory :emailCategorySet){
                    if(emailCategory != '-null-'){
                        emailCategoryMap.put(emailCategory, psec);
                    }else{
                        emailCategoryMap.put(null, psec);
                    }
                }
            }

             if(!psec.caseCategoryList.isEmpty() && psec.caseCategoryList!= Null){
                for(String caseCategory :caseCategorySet){
                    if(caseCategory != '-null-'){
                        caseCategoryMap.put(caseCategory, psec);
                    }else{
                        caseCategoryMap.put(null, psec);
                    }
                }
             } 
            sectionList.add(psec);                           
        }  

         if(sectionList.isEmpty()){
            return;
        }


    }


}
1
  • Did you try to access the wrapper in your test class and having any access issues?
    – highfive
    Commented Apr 2, 2014 at 6:11

3 Answers 3

16

Your inner class should be public instead of private.

Controller

public with sharing class ViewRelatedController 
{
    public class PageSection 
    {
         public String name {get; private set;} 
    }
}

Test Class

@isTest
private class TestViewRelatedController 
{
    public static void myUnitTest()
    {
        ViewRelatedController.PageSection page = new ViewRelatedController.PageSection();
        page.name = 'Hello';
        System.debug('>>>> Section Name '+page.Name);
    }
}
1
  • Instead of hardcoding the value in PageSection member variables, can we make entries to My_Config__c. because we are looping throgh the config object and giving the value to pagesection member variable. Commented Apr 2, 2014 at 6:59
6

If you are talking about access issue to the inner class from your test class, you can try other answers posted here or just add @TestVisible to your inner class.

@TestVisible
private class PageSection {
    //your code
}
2

You could create a new instance of your class. First you have to change this to public, then just create a new instance and call the methods of this class:

@isTest
private class PageSectionTest {
    static testMethod void testBasic() {
        ViewRelatedController.PageSection psec = new ViewRelatedController.PageSection();
        psec.emailCategoryList = new List<String>();
        // and so on calling methods of your calls
    }
}

Finally, you should consider to test the method assertion by adding asserts to control if they are returning what is expected.

1
  • hello Martin!! on the same basis i have create a list<psec.emailCategoryList> and now i want to add the value in this,how can i do?? Please help me. Commented May 26, 2017 at 9:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .