need help with test class with ApexPages.currentPage().getParameters().id

  • How can I test the below controller and get greater than 0% coverage?

    My controller class looks like this:

    public with sharing class EmployeeController 
    {
    
       public EmployeeController(ApexPages.StandardController controller) 
       {
    
        }
    
        public PageReference pdf1()
        {   
          return Page.Employee_PDF;
        }
    
    
    
         public list<Certification__c> certification
         {
            get
            {
                    certification = [SELECT Date_Certified__c, Name, Principle__c, Year_Certified__c FROM Certification__c WHERE Employee__c =:ApexPages.CurrentPage().getparameters().get('id')];
                    return certification;
            }
            set;
         }
    
        public list<Project_Experience__c> proj
            {
                    get
                    {
                            proj = [SELECT Client__c, Duration__c, Location__c, Position__c, Project_Description__c, Project_Name__c, Responsibilities__c FROM Project_Experience__c WHERE Employee__c =:ApexPages.CurrentPage().getparameters().get('id')order by Project_End_Date__c desc];
                            return proj;
                    }
                    set;
            }
    
        public list<Training__c> training
        {
            get
            {
                    training =[SELECT Location__c, Name, Principle__c, Trainer__c, Year__c FROM Training__c WHERE Employee__c =:ApexPages.CurrentPage().getparameters().get('id')];
                    return training;
            }
            set;
        }
    
            public list<Employee__c> emp
            {
                    get
                    {
                            emp = [SELECT Name, Sex__c, Foreign_Language_Proficiency__c, Nationality__c, Qualifications__c, Tools__c FROM Employee__c where Id = :ApexPages.CurrentPage().getparameters().get('id') ];
                            return emp;
                    }
                    set;
            }
       }
    

    Here is my current test class:

    @isTest
    private class EmployeeControllerTestClass2 {
        static testMethod void myUnitTest() 
        {
           Employee__c emp = new Employee__c(Name = 'empTest',Employee_Status__c='Active' );
            insert emp;
    
            Project_Experience__c project = new Project_Experience__c (Employee__c = emp.id, Project_Name__c='Indosat');
            insert project;
    
           Training__c training = new Training__c (Employee__c = emp.id, Name='Salesforce Adm 201');
           insert training;
    
    
            PageReference pageRef = Page.Employee_Pages;
            Test.setCurrentPage(pageRef);
            ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(emp);
            ApexPages.currentPage().getParameters().put('Id',emp.id);
    
            Test.StartTest();
        }
    }
    

    It may help if you express what exactly is confusing you. Looking at your testcode I think you've got the hang of it, but still need to do it for all parameters, and set them before you initiate the controller.

  • To get code coverage for the EmployeeController you need to call the constructor and methods from the test case.

    E.g.

    // ...
    
    PageReference pageRef = Page.Employee_Pages;
    Test.setCurrentPage(pageRef);
    ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(emp);
    ApexPages.currentPage().getParameters().put('Id',emp.id);
    
    EmployeeController ec = new EmployeeController(sc);
    

    Then you can call some of the properties to get code coverage for them as well. Make some assertions based on what you setup in the start of the test case.

    list<Employee__c> emps = ec.emp;
    System.assertEquals(1, emps.size();
    
    list<Project_Experience__c> projs = ec.proj;
    System.assertEquals(1, projs.size();
    

    You might like to extract the Id from the parameters in the constructor once and then use the member in the properties.

    public with sharing class EmployeeController {
    
        private Id empId;
    
        public EmployeeController(ApexPages.StandardController controller) {
            empId = ApexPages.CurrentPage().getparameters().get('id');
        }
    
        //...
        public list<Certification__c> certification {
            get {
                if(certification == null) {
                    certification = [SELECT Date_Certified__c, Name, Principle__c, Year_Certified__c 
                                     FROM Certification__c WHERE Employee__c = :empId];
                }
                return certification;
            }
            private set;
        }
        //...
    }
    

    Hi Daniel, have tried your suggestion. but it still return with 0% coverage test result. still don't know what i miss in here.

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM

Tags used