+ Start a Discussion
Bablu Kumar PanditBablu Kumar Pandit 

How to Cover test Class of Following Vf controller

-----------------Vf controller-----------
Public class THGenesisFeedbackController{

    public String Ques1{set;get;}
    public String Ques2{set;get;}
    public String Ques3{set;get;}
    public String Ques4{set;get;}
    public String Ques5{set;get;}
    public String Ques7{set;get;}
    public String CaseId{set;get;}
    public String rating1{set;get;}
    public String rating2{set;get;}
    public String rating3{set;get;}
    public String rating4{set;get;}
    public String rating5{set;get;}
    public String rating6{set;get;}
    public List<Case> lstCase{set;get;}
    //public String QueTest{set;get;}
    public Boolean caseSubmitted {set;get;}
    public String logo{set;get;}
    
    public THGenesisFeedbackController(){
        CaseId = ApexPages.currentPage().getParameters().get('CaseId');
        lstCase = new List<Case>();
        if(CaseId!=null){
                   lstCase = [Select id,ContactId,Contact.Email,overall_satisfaction__c,Dissatisfaction_Reason__c, Product__C,Presales_Experience__c, Upsell_Experience__c,Number_Of_Times_Contacted__c,
                    Product_Usage_Experience__c,Cost_Performance__c,Product_Quality__c,Aftersales_Service__c, Recommendation_To_Friend__c,Recommendation_To_Friend_Reason__c,
                    Other_feedback__c,Feedback_Submitted__c,CSat_Rating__c ,CSat_Company_Feedback__c from Case where id=:CaseId];
                    System.debug('logo-->'+lstcase[0].Product__C);

            System.debug('logo-->'+lstcase[0].Product__C);
            if(!lstCase.isEmpty()){
                if(lstcase[0].Product__C!=null){
                    logo = lstcase[0].Product__C;
                }
                caseSubmitted = lstcase[0].Feedback_Submitted__c;

            }
        }
        Ques1 = '';
        Ques2 = '';
        Ques3 = '';
        Ques4 = '';
        Ques5 = '';
        Ques7 = '';
        rating1 = '';
        rating2 = '';
        rating3 = '';
        rating4 = '';
        rating5 = '';
        rating6 = '';
        //logo ='';
    }    
    public PageReference CustomerFeedback(){
        System.debug('CaseId-->'+CaseId);
        if(CaseId!=null){
        lstCase = [Select id,ContactId,Contact.Email,overall_satisfaction__c,Dissatisfaction_Reason__c, Product__C,Presales_Experience__c, Upsell_Experience__c,Number_Of_Times_Contacted__c,
        Product_Usage_Experience__c,Cost_Performance__c,Product_Quality__c,Aftersales_Service__c, Recommendation_To_Friend__c,Recommendation_To_Friend_Reason__c,
        Other_feedback__c,Feedback_Submitted__c,CSat_Rating__c ,CSat_Company_Feedback__c from Case where id=:CaseId];
        System.debug('logo-->'+lstcase[0].Product__C);
            if(!lstCase.isEmpty()){
                if(lstcase[0].Product__C!=null)
                    logo = lstcase[0].Product__C;
                if(Ques1 != null && Ques1 == 'Extremely Satisfied'){
                    lstcase[0].CSat_Rating__c = 5;
                }else if(Ques1 != null && Ques1 == 'Very Satisfied'){
                    lstcase[0].CSat_Rating__c = 4;
                }else if(Ques1 != null && Ques1 == 'Neither satisfied nor dissatisfied'){
                    lstcase[0].CSat_Rating__c = 3;
                }else if(Ques1 != null && Ques1 == 'Somewhat dissatisfied'){
                    lstcase[0].CSat_Rating__c = 2;
                }else if(Ques1 != null && Ques1 == 'Very dissatisfied'){
                    lstcase[0].CSat_Rating__c = 1;
                }
                lstCase[0].Dissatisfaction_Reason__c= ( Ques2 != null ? Ques2 : '');
                lstCase[0].Number_Of_Times_Contacted__c= ( Ques3 != null ? Ques3 : '');
                lstCase[0].Recommendation_To_Friend__c= ( Ques4 != null ? Ques4 : '');
                lstCase[0].Recommendation_To_Friend_Reason__c= ( Ques5 != null ? Ques5 : '');
                lstCase[0].CSat_Company_Feedback__c = (Ques7!= Null ? Ques7 : '');
                lstCase[0].Presales_Experience__c = (rating1!= Null ? rating1 : '');
                lstCase[0].Upsell_Experience__c = (rating2!= Null ? rating2 : '');
                lstCase[0].Product_Usage_Experience__c = (rating3!= Null ? rating3 : '');
                lstCase[0].Product_Quality__c = (rating4!= Null ? rating4 : '');
                lstCase[0].Cost_Performance__c = (rating5!= Null ? rating5 : '');
                lstCase[0].Aftersales_Service__c = (rating6!= Null ? rating6 : '');
                if(Ques1 != NUll){
                    lstcase[0].Feedback_Submitted__c = true;
                }else if(Ques2 != null){
                    lstcase[0].Feedback_Submitted__c = true;
                }else if(Ques3 != Null){
                    lstcase[0].Feedback_Submitted__c = true;
                }else if(Ques4 != Null){
                    lstcase[0].Feedback_Submitted__c = true;
                }else if(Ques5 != Null){
                    lstcase[0].Feedback_Submitted__c = true;
                }else if(Ques7 != Null){
                    lstcase[0].Feedback_Submitted__c = true;
                }else if(rating1 != Null){
                    lstcase[0].Feedback_Submitted__c = true;
                }else if(rating2 != Null){
                    lstcase[0].Feedback_Submitted__c = true;
                }else if(rating3 != Null){
                    lstcase[0].Feedback_Submitted__c = true;
                }else if(rating4 != Null){
                    lstcase[0].Feedback_Submitted__c = true;
                }else if(rating5 != Null){
                    lstcase[0].Feedback_Submitted__c = true;
                }else if(rating6 != Null){
                    lstcase[0].Feedback_Submitted__c = true;
                }
                update lstcase;
            }
        }
        return new PageReference('javascript:window.close()');
    }
}

------ My Test Class---
//--it only Cover 70%-------
@isTest
public class THGenesisFeedbackControllerTest {
    Static testmethod void feedbacktest(){
        Case objcase = new Case();
        objcase.Product__C = 'Digestive Center';
        objcase.overall_satisfaction__c = 'Extremely Satisfied';
        objcase.CSat_Rating__c = 5;
        objcase.Dissatisfaction_Reason__c = 'Test';
        objcase.Number_Of_Times_Contacted__c = '3';
        objcase.Recommendation_To_Friend__c = '4';
        objcase.Recommendation_To_Friend_Reason__c = 'Good Product';
        objcase.Presales_Experience__c = '10';
        objcase.Upsell_Experience__c = '9';
        objcase.Product_Usage_Experience__c = '8';
        objcase.Product_Quality__c = '7';
        objcase.Cost_Performance__c = '5';
        objcase.Aftersales_Service__c = '4';
        objcase.Feedback_Submitted__c = true;
        objcase.CSat_Company_Feedback__c = 'Test';
        insert objcase;
           
        update objcase;
        test.startTest();
        
            PageReference pageRef = Page.THGenesisFeedback;
            pageRef.getParameters().put('CaseId', String.valueOf(objcase.Id));
            Test.setCurrentPage(pageRef);
            
            THGenesisFeedbackController objThgenesis = New THGenesisFeedbackController();
            objThgenesis.CustomerFeedback();
            
        test.stopTest();
    }
}
vishal-negandhivishal-negandhi

IHello Bablu, 

A lot of your code is depending on the values in Ques* and rating* fields, so make sure you also assign them values in your test class
Example:
 

THGenesisFeedbackController objThgenesis = New THGenesisFeedbackController();
objThgenesis.Ques1 = 'Extremely Satisfied';
objThgenesis.CustomerFeedback();

objThgenesis = New THGenesisFeedbackController();
objThgenesis.Ques1 = 'Very Satisfied';
objThgenesis.CustomerFeedback();

do the same for all your other if conditions

I hope this helps.

 

Best,

Vishal

Bablu Kumar PanditBablu Kumar Pandit
hii Vishan Thanku so much it worked for me now i have problem In Below Part that not Cover
can you Please help me..
}else if(Ques2 != null){
                    lstcase[0].Feedback_Submitted__c = true;
                }else if(Ques3 != Null){
                    lstcase[0].Feedback_Submitted__c = true;
                }else if(Ques4 != Null){
                    lstcase[0].Feedback_Submitted__c = true;
                }else if(Ques5 != Null){
                    lstcase[0].Feedback_Submitted__c = true;
                }else if(Ques7 != Null){
                    lstcase[0].Feedback_Submitted__c = true;
                }else if(rating1 != Null){
                    lstcase[0].Feedback_Submitted__c = true;
                }else if(rating2 != Null){
                    lstcase[0].Feedback_Submitted__c = true;
                }else if(rating3 != Null){
                    lstcase[0].Feedback_Submitted__c = true;
                }else if(rating4 != Null){
                    lstcase[0].Feedback_Submitted__c = true;
                }else if(rating5 != Null){
                    lstcase[0].Feedback_Submitted__c = true;
                }else if(rating6 != Null){
                    lstcase[0].Feedback_Submitted__c = true;
                }

Now New test Class is
@isTest
public class THGenesisFeedbackControllerTest {
    Static testmethod void feedbacktest(){
        Case objcase = new Case();
        objcase.Product__C = 'Digestive Center';
        //objcase.overall_satisfaction__c = 'Extremely Satisfied';
        //objcase.CSat_Rating__c = 5;
        objcase.Dissatisfaction_Reason__c = 'Test';
        objcase.Number_Of_Times_Contacted__c = '3';
        objcase.Recommendation_To_Friend__c = '4';
        objcase.Recommendation_To_Friend_Reason__c = 'Good Product';
        objcase.Presales_Experience__c = '10';
        objcase.Upsell_Experience__c = '9';
        objcase.Product_Usage_Experience__c = '8';
        objcase.Product_Quality__c = '7';
        objcase.Cost_Performance__c = '5';
        objcase.Aftersales_Service__c = '4';
        objcase.Feedback_Submitted__c = true;
        objcase.CSat_Company_Feedback__c = 'Test';
        insert objcase;
           
        test.startTest();
        
            PageReference pageRef = Page.THGenesisFeedback;
            pageRef.getParameters().put('CaseId', String.valueOf(objcase.Id));
            Test.setCurrentPage(pageRef);
            
            THGenesisFeedbackController objThgenesis = New THGenesisFeedbackController();
            objThgenesis.Ques1 = 'Extremely Satisfied';
            objThgenesis.CustomerFeedback();
        
            objThgenesis = New THGenesisFeedbackController();
            objThgenesis.Ques1 = 'Very Satisfied';
            objThgenesis.CustomerFeedback();
            
         	objThgenesis = New THGenesisFeedbackController();
            objThgenesis.Ques1 = 'Neither satisfied nor dissatisfied';
            objThgenesis.CustomerFeedback();
           
        	objThgenesis = New THGenesisFeedbackController();
            objThgenesis.Ques1 = 'Somewhat dissatisfied';
            objThgenesis.CustomerFeedback();
        
        	objThgenesis = New THGenesisFeedbackController();
            objThgenesis.Ques1 = 'Very dissatisfied';
            objThgenesis.CustomerFeedback();
        
        	THGenesisFeedbackController objThgenesis1 = New THGenesisFeedbackController();
        	objThgenesis1.Ques2 = 'Test';
        	objThgenesis1.CustomerFeedback();
                   
        test.stopTest();
    }
}

Thanks in advance.