+ Start a Discussion
sumit dsumit d 

Test class coverage in VF page controller

Hi All,
          I have a controller and I wrote its test class. it's not covering some part.
My controller is given below:-
 
public without sharing class MatchMakingController {
    private ApexPages.StandardController sctrl;
    rie__Registration__c regObj;
    public Map<String, List<SelectOption>> questionsMap {get;set;}
    private Map<String, String> questionAnswerMap;
    public Map<String, rie__Question__c> questionsSet {get;set;}
    public String Selectedvalues {get;set;}
    public String answeredQuestion{get; set;}
    
    public MatchMakingController(ApexPages.StandardController st){
        this.sctrl = st;
        this.regObj = (rie__Registration__c) st.getRecord();
        questionAnswerMap = new Map<String, String>();
        
    }
  
     public void populateAnswers()
    {
        questionAnswerMap.put(answeredQuestion.trim(), Selectedvalues.trim());
        System.debug('***'+questionAnswerMap);
        Selectedvalues=Null;
    }
    
    public pageReference submitAnswers(){
        List<rie__Registrant_Answer__c> existingRegAnswersList = new List<rie__Registrant_Answer__c>([Select id, rie__Question__c,
                                                                                                     rie__Answer__c, rie__Registration__c
                                                                                                     from rie__Registrant_Answer__c
                                                                                                     where rie__Registration__c =: regObj.Id]);
        Map<String, rie__Registrant_Answer__c> existingRegAnswersMap = new Map<String, rie__Registrant_Answer__c>();
        if(existingRegAnswersList != Null && existingRegAnswersList.size() > 0){
            for(rie__Registrant_Answer__c regAnswers : existingRegAnswersList){
                existingRegAnswersMap.put(regAnswers.rie__Question__c, regAnswers);
            }
        }
        system.debug('questionAnswerMap---->'+questionAnswerMap);
        List<rie__Registrant_Answer__c> regAnswerList = new List<rie__Registrant_Answer__c>();
        if(questionAnswerMap != Null && questionAnswerMap.size() > 0){
            for(String question : questionAnswerMap.keySet()){
                if(question != Null && question != 'None' &&
                   questionAnswerMap.get(question) != Null && 
                   questionAnswerMap.get(question) != 'None'){
                       if(existingRegAnswersMap != Null && 
                          existingRegAnswersMap.size() > 0 && 
                          existingRegAnswersMap.containsKey(question)){
                              rie__Registrant_Answer__c regAnswer = new rie__Registrant_Answer__c(Id = existingRegAnswersMap.get(question).Id);
                              regAnswer.rie__Question__c = questionsSet.get(question).Id;
                              regAnswer.rie__Answer__c = questionAnswerMap.get(question);
                              regAnswer.rie__Registration__c = regObj.Id;
                              regAnswerList.add(regAnswer); 
                          }else{
                              rie__Registrant_Answer__c regAnswer = new rie__Registrant_Answer__c();
                              regAnswer.rie__Question__c = questionsSet.get(question).Id;
                              regAnswer.rie__Answer__c = questionAnswerMap.get(question);
                              regAnswer.rie__Registration__c = regObj.Id;
                              regAnswerList.add(regAnswer);  
                          }
                       
                       
                   }
            }
        }
        if(regAnswerList != Null && regAnswerList.size() > 0){
            insert regAnswerList;
        }
        Selectedvalues=Null;
        PageReference cancel = sctrl.cancel(); 
        return cancel;
    }
    
    public pageReference cancel(){
       PageReference cancel = sctrl.cancel(); 
       return cancel; 
    }
}

Test class is given below:-
@istest
private class MatchMakingControllerTest {
   
    Static testmethod void MatchMakingMethod(){
           String txt = 'THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG';
         rie__Event__c evt = new rie__Event__c();
        evt.rie__Event_Time_Zone__c = '(GMT-07:00) Pacific Daylight Time (America/Los_Angeles)';
        insert evt;
        
        rie__Registration__c attendee = new rie__Registration__c();
        attendee.rie__BatchProcessedForMatchMaking__c = false;
        attendee.rie__Event__c = evt.id;
        insert attendee;
        
        rie__Question__c qus = new rie__Question__c();
        qus.rie__Question__c  = 'Are you looking for a Solution?';
        qus.rie__Answers__c = 'ans,yes';
        insert qus;
        
        rie__Registrant_Answer__c regAns =  new rie__Registrant_Answer__c();
        regAns.rie__Question__c = qus.id;
        regAns.rie__Registration__c =  attendee.id;
        regAns.rie__Answer__c = 'yes';
        insert regAns;
        
        test.startTest();
        PageReference pageRef  = Page.MatchMakingQuestions; 
        pageRef.getParameters().put('Id', regAns.id);
        Test.setCurrentPage(pageRef);
        Registration__c reg = new Registration__c();
        Apexpages.StandardController sc = new Apexpages.standardController(reg);
        
        // Instantiate the extension
        MatchMakingController ext = new MatchMakingController(sc);
        ext.answeredQuestion = txt;
        //ext.populateAnswers();
        PageReference testPageResults = ext.cancel();
        PageReference testPageResult = ext.getQuestionsList();
        PageReference testPageRe = ext.submitAnswers();
        test.stoptest();
        
    }
}
how can I cover bold lines in my test class?
Can anyone help me with this?
​​​​​​​
Best Answer chosen by sumit d
Janet EpebinuJanet Epebinu
Hi Sumit,
Try to change the parameter of Apexpages.StandardController sc = new Apexpages.standardController(reg) on line 31 to an instance of  rie__Registration__c that is, attendee, instead of that of Registeration__c

All Answers

SwethaSwetha (Salesforce Developers) 
HI Sumit,
The below articles give an idea of how code coverage can be improved. You will need to customize based on your requirement
 
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines
 
https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test
 
Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you
sumit dsumit d
Hi Swetha,
                  I have updated my test class. I am getting this error: -System.NullPointerException: Attempt to de-reference a null object At line 52
 regAnswer.rie__Question__c = questionsSet.get(question).Id;

How can I remove this error.

My test class is given below:-
@istest
private class MatchMakingControllerTest {
   
    Static testmethod void MatchMakingMethod(){
        String txt = 'THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG';
        rie__Event__c evt = new rie__Event__c();
        evt.rie__Event_Time_Zone__c = '(GMT-07:00) Pacific Daylight Time (America/Los_Angeles)';
        insert evt;
        
        rie__Registration__c attendee = new rie__Registration__c();
        attendee.rie__BatchProcessedForMatchMaking__c = false;
        attendee.rie__Event__c = evt.id;
        insert attendee;
        
        rie__Question__c qus = new rie__Question__c();
        qus.rie__Question__c  = 'Are you looking for a Solution?';
        qus.rie__Answers__c = 'ans,yes';
        insert qus;
        Map<String, rie__Question__c> questionSet = new Map<String, rie__Question__c>{'Are you looking for a Solution?' => qus};
        //
        rie__Registrant_Answer__c regAns =  new rie__Registrant_Answer__c();
        regAns.rie__Question__c = qus.id;
        regAns.rie__Registration__c =  attendee.id;
        regAns.rie__Answer__c = 'yes';
        insert regAns;
        
        test.startTest();
        PageReference pageRef  = Page.MatchMakingQuestions; 
        pageRef.getParameters().put('Id', attendee.id);
        Test.setCurrentPage(pageRef);
        Registration__c reg = new Registration__c();
        Apexpages.StandardController sc = new Apexpages.standardController(reg);
        
        // Instantiate the extension
        MatchMakingController ext = new MatchMakingController(sc);
        ext.answeredQuestion = txt;
        ext.Selectedvalues = txt;
        ext.questionsSet = questionSet;
        
        ext.populateAnswers();
        PageReference testPageResults = ext.cancel();
        PageReference testPageResult = ext.getQuestionsList();
        PageReference testPageRe = ext.submitAnswers();
       // testPageRe.getParameters().put('Are you looking for a Solution?', qus);
        test.stoptest();
        
    }
    
}
can you help me how to remove this ?
Janet EpebinuJanet Epebinu
Hi Sumit,
Try to change the parameter of Apexpages.StandardController sc = new Apexpages.standardController(reg) on line 31 to an instance of  rie__Registration__c that is, attendee, instead of that of Registeration__c
This was selected as the best answer
sumit dsumit d
Hi Janet,
               I tried this but I am still getting the error. Can you help me with this?
Janet EpebinuJanet Epebinu
Hi Sumit,
These are things I will suggest you do:
1. Using the former test class, change the parameter on line 31 to an instance of rie_Registration__c and run the test, OR
2. On the updated test class, comment out the line that is giving the error and use System.debug() to check the result of the line before it, 
3. Going through the updated test class, you assigned ext.answeredQuestion and ext.Selectedvalues to the same variable (ext.answeredQuestion = txt; and ext.Selectedvalues = txt).. You can change one to "txt1"
At times, the error on a line of code may be as a result of an error that occurs before it.
sumit dsumit d
Hi Janet ,
                 the full controller is given below:-
public without sharing class MatchMakingController {
    private ApexPages.StandardController sctrl;
    rie__Registration__c regObj;
    public Map<String, List<SelectOption>> questionsMap {get;set;}
    private Map<String, String> questionAnswerMap;
    public Map<String, rie__Question__c> questionsSet {get;set;}
    public String Selectedvalues {get;set;}
    public String answeredQuestion{get; set;}
    
    public MatchMakingController(ApexPages.StandardController st){
        this.sctrl = st;
        this.regObj = (rie__Registration__c) st.getRecord();
        questionAnswerMap = new Map<String, String>();
        
    }
    
    public pageReference getQuestionsList(){
       List<rie__Question__c> questionsList = new List<rie__Question__c>([Select id, rie__Question__c, 
                                                   						  rie__Answers__c 
                                                                          from rie__Question__c]);
        questionsMap = new Map<String, List<SelectOption>>();
        List<SelectOption> answerList = new List<SelectOption>();
        questionsSet = new Map<String, rie__Question__c>();
        if(questionsList != Null && questionsList.size() > 0){
            for(rie__Question__c questions : questionsList){
                answerList = new List<SelectOption>();
                if(questions.rie__Answers__c.contains(',')){
                    List<String> answerSplitList = new List<String>(questions.rie__Answers__c.split(','));
                    if(answerSplitList != Null && answerSplitList.size() > 0){
                        answerList.add(new SelectOption('None','--NONE--'));
                        for(String str : answerSplitList){
                           answerList.add(new SelectOption(str.trim(), str.trim())); 
                        }
                    }
                    questionsMap.put(questions.rie__Question__c.trim() , answerList);
                    questionsSet.put(questions.rie__Question__c.trim() , questions); 
                }
                
            }
        }
        return null;
        
    }
    
     public void populateAnswers()
    {
        questionAnswerMap.put(answeredQuestion.trim(), Selectedvalues.trim());
        System.debug('***'+questionAnswerMap);
        Selectedvalues=Null;
    }
    
    public pageReference submitAnswers(){
        List<rie__Registrant_Answer__c> existingRegAnswersList = new List<rie__Registrant_Answer__c>([Select id, rie__Question__c,
                                                                                                     rie__Answer__c, rie__Registration__c
                                                                                                     from rie__Registrant_Answer__c
                                                                                                     where rie__Registration__c =: regObj.Id]);
        Map<String, rie__Registrant_Answer__c> existingRegAnswersMap = new Map<String, rie__Registrant_Answer__c>();
        if(existingRegAnswersList != Null && existingRegAnswersList.size() > 0){
            for(rie__Registrant_Answer__c regAnswers : existingRegAnswersList){
                existingRegAnswersMap.put(regAnswers.rie__Question__c, regAnswers);
            }
        }
        system.debug('questionAnswerMap---->'+questionAnswerMap);
        List<rie__Registrant_Answer__c> regAnswerList = new List<rie__Registrant_Answer__c>();
        if(questionAnswerMap != Null && questionAnswerMap.size() > 0){
            for(String question : questionAnswerMap.keySet()){
                if(question != Null && question != 'None' &&
                   questionAnswerMap.get(question) != Null && 
                   questionAnswerMap.get(question) != 'None'){
                       if(existingRegAnswersMap != Null && 
                          existingRegAnswersMap.size() > 0 && 
                          existingRegAnswersMap.containsKey(question)){
                              rie__Registrant_Answer__c regAnswer = new rie__Registrant_Answer__c(Id = existingRegAnswersMap.get(question).Id);
                              regAnswer.rie__Question__c = questionsSet.get(question).Id;
                              regAnswer.rie__Answer__c = questionAnswerMap.get(question);
                              regAnswer.rie__Registration__c = regObj.Id;
                              regAnswerList.add(regAnswer); 
                          }else{
                              rie__Registrant_Answer__c regAnswer = new rie__Registrant_Answer__c();
                              regAnswer.rie__Question__c = questionsSet.get(question).Id;
                              regAnswer.rie__Answer__c = questionAnswerMap.get(question);
                              regAnswer.rie__Registration__c = regObj.Id;
                              regAnswerList.add(regAnswer);  
                          }
                       
                       
                   }
            }
        }
        if(regAnswerList != Null && regAnswerList.size() > 0){
            insert regAnswerList;
        }
        Selectedvalues=Null;
        PageReference cancel = sctrl.cancel(); 
        return cancel;
    }
    
    public pageReference cancel(){
       PageReference cancel = sctrl.cancel(); 
       return cancel; 
    }
}

Can you get any idea how to remove this error?
sumit dsumit d
Hi Janet,
      I solved it. thanks