+ Start a Discussion
sfdc@isha.ax1814sfdc@isha.ax1814 

Need help on test class

Hi everyone,

i am trying to write the testclass on below class.  some one help me on the test class. I have tried the below given test class but got 0% coverage



public class Trendingfeedbackiconextension{
 
    
    public Id RecordId{get;set;}       
    public String param1 { get; set; }
    public String buttonText{get;set;}
    public string reportComponentName{get;set;}
    public string reportComponentUniqueName{get; 
            set {
            reportComponentUniqueName = value;
            Trendingfeedbackhelperextensionmethod();
            }}
              
    Map<String,Integer> likedislikeCountMap = new Map<String,Integer>();
     
    public pagereference likeDislikemethod(){
     likedislikeCountMap.clear();
    RecordId= Apexpages.currentPage().getParameters().get('id');
    
    System.debug('@@'+RecordId);   
         system.debug('reportComponentUniqueName==>'+reportComponentUniqueName );
        system.debug('reportComponentName==>'+reportComponentName);
        string value= Apexpages.currentPage().getParameters().get('param1');
        system.debug('param1==>'+param1);  
        string componentName = param1.split('-')[0];
        string actionName = param1.split('-')[1];  
        system.debug('componentName==>'+componentName);    
        system.debug('actionName==>'+actionName);    
        //sentdata= Userinfo.getUserId();
        system.debug('@@'+value);
        list<Feedback__c> flist= new list<Feedback__c>();
        Feedback__c f=new Feedback__c();
        flist=[select id,Name,Action__c,User__c,Comment__c from Feedback__c where Name=:componentName And ((Action__c='Like' Or Action__c='Dislike') And User__c=:Userinfo.getUserId()) And AccountId__c=:RecordId];
        system.debug('@@'+flist);
        if(flist.size()== 0){
            system.debug('@@'+flist.size());
            f.Action__c = actionName;
            //sendAction = f.Action__c;
            f.Name = componentName;
            f.AccountId__c=RecordId;
            f.Comment__c=buttonText;
            f.User__c = UserInfo.getUserId();
            system.debug('f===>'+f);
            insert f;

        }
        else {
            system.debug('elseenter');
            for(Feedback__c f2:flist){
                f2.Action__c = actionName;
                f2.Comment__c= buttonText;
              /*  if(f2.Action__c == 'Like'){
                    system.debug('@@'+likedata);
                    system.debug('@@'+dislikedata);
                    likedata =True;
                    dislikedata = false;

                }
                else if(f2.Action__c == 'Dislike'){
                    likedata =false;
                    dislikedata = true;

                }*/

            }
            update flist;
            Trendingfeedbackhelperextensionmethod();
        }
         buttonText='';
        return null;

    }


    

    public void Trendingfeedbackhelperextensionmethod(){ 

     RecordId= Apexpages.currentPage().getParameters().get('id');   
     System.debug('@@'+RecordId);   
       
        system.debug('method enter');
        system.debug('reportComponentUniqueName==>'+reportComponentUniqueName );
        system.debug('reportComponentName==>'+reportComponentName);
        system.debug('@@'+RecordId);
        //list<AggregateResult> results = [select count(Id) Quantity,Action__c FROM Feedback__c WHERE Name In('AORT1','AORT2') GROUP BY  Action__c];        
        list<AggregateResult> results = [select count(Id) Quantity,Action__c FROM Feedback__c WHERE Name =:reportComponentUniqueName and AccountId__c=:RecordId GROUP BY  Action__c having Action__c='Like' OR Action__c='Dislike'];        

        system.debug('@@'+results);
        for(AggregateResult ar : results){
            system.debug('@@'+results);            
            likedislikeCountMap.put((string)ar.get('Action__c'), (integer)ar.get('Quantity'));
            system.debug('@@'+likedislikeCountMap); 
        
       getLikeCount();
       getDislikeCount();
        

        }              

    }
   
    public Trendingfeedbackiconextension(){
   Trendingfeedbackhelperextensionmethod();    
       //system.debug('method enter');
    }
        
/*

        //list<AggregateResult> results = [select count(Id) Quantity,Action__c FROM Feedback__c WHERE Name In('AORT1','AORT2') GROUP BY  Action__c];        
        list<AggregateResult> results = [select count(Id) Quantity,Action__c FROM Feedback__c WHERE Name ='AccountOrganizationRevenueTrend' GROUP BY  Action__c];        

        system.debug('@@'+results);
        for(AggregateResult ar : results){
            system.debug('@@'+results);            
            likedislikeCountMap.put((string)ar.get('Action__c'), (integer)ar.get('Quantity'));
            system.debug('@@'+likedislikeCountMap); 
        }
    
    }*/

     public Integer getLikeCount(){
        if(likedislikeCountMap.get('Like') == null) {
        system.debug('like');
            return 0;
        } 
        return likedislikeCountMap.get('Like');
        
        
    }  
    public Integer getDislikeCount(){
    
        if(likedislikeCountMap.get('Dislike') == null) {
        system.debug('Dislike');
            return 0;
        }
        return likedislikeCountMap.get('Dislike');
    }


    public PageReference Save() {
    string value2= Apexpages.currentPage().getParameters().get('param2');
        string componentName = value2.split('-')[0];
        string actionName = value2.split('-')[1];
        list<Feedback__c> f=new list<Feedback__c>();
        Feedback__c fb=new Feedback__c();
        fb.Action__c=actionName;
        fb.Name=componentName;    
        fb.Comment__c=buttonText;
        fb.AccountId__c=Apexpages.currentPage().getParameters().get('id');
        fb.User__c=userinfo.getuserid();
        f.add(fb);
        insert f; 

        buttonText='';
        return null;
       }




}

Test class:

@isTest
public class TrendingfeedbackiconextensionTest {
    
private static testMethod void trendingfeedbacktest() {

    //list<Feedback__c> f1 = new list<Feedback__c>();
    Feedback__c f2 = new Feedback__c();
    f2.AccountId__c = 'testid';
    f2.Action__c = 'Like';
    f2.Comment__c = 'testcomment';
    f2.User__c = UserInfo.getUserId();
   // f1.add(f2);
    insert f2;
    
    f2.Action__c= 'Dislike';
    
    Update f2;
  
   
    
}
}


Regards,
Isha
SalesforceDevil@16SalesforceDevil@16
Hello Isha ,

you can refer this link because of it is a extension controller you just need to set the vf page check out this reference it will help you sure
https://blog.jeffdouglas.com/2010/06/02/testing-salesforce-com-controller-extensions/
https://salesforce.stackexchange.com/questions/102740/how-to-write-test-class-for-standard-controller-along-with-extensions


Thanx Regards 
Shubham
Raj VakatiRaj Vakati
Try this

 
@isTest
public class TrendingfeedbackiconextensionTest {
    
private static testMethod void trendingfeedbacktest() {

	Profile profile1 = [Select Id from Profile where name = 'System Administrator'];
       System.debug('What is the profile id ' + profile1);
       UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
       date tDate = date.today();
       date uDate = Date.today().addDays(30);
        
         User u = new User(
            UserRoleId = portalRole.Id,
            ProfileId = profile1.Id,
            Username = 'testtermsconditions1234423@asdasdasdasd.com',
            Alias = 'batman',
            Email='testtermsconditions1234423@asdasdasd.com',
            EmailEncodingKey='UTF-8',
            Firstname='Bruce',
            Lastname='Wayne',
            LanguageLocaleKey='en_US',
            LocaleSidKey='en_US',
            TimeZoneSidKey='America/Chicago');
            insert u;

     System.runAs(u){
		 
		  Account a = new Account();
        	a.Name= 'Test SP Account';
        	a.Type= 'Dealer / Distributor';
         
        insert a;
		
		
    Feedback__c f2 = new Feedback__c();
    f2.AccountId__c = a.Id;
	    f2.Name = 'Like';

    f2.Action__c = 'Like';
    f2.Comment__c = 'testcomment';
    f2.User__c = UserInfo.getUserId();
   // f1.add(f2);
    insert f2;
    
    
   PageReference  pref = Page.MoveRecords;
    pref.getParameters().put('param1','Like');
	    pref.getParameters().put('param2','Dislike');

	 pref.getParameters().put('Id',f2.Id);
    Test.setCurrentPage(pref);
 Trendingfeedbackiconextension fee = new Trendingfeedbackiconextension() ;
 fee.likeDislikemethod();
 
 fee.Trendingfeedbackhelperextensionmethod();
 fee.getLikeCount();
 fee.getDislikeCount();
 fee.Save() ;
 
  
	 }
    
}
}

 
sfdc@isha.ax1814sfdc@isha.ax1814
Hi Ravi,

Here iam using a vf compoenent and thet component is using in various pages.

please find below vf component code.

<apex:component controller="Trendingfeedbackiconextension" allowDML="true">
   <!-- <apex:includeScript value="{!URLFOR($Resource.jQuery, '/js/jquery-1.4.2.min.js')}"  />
<apex:stylesheet value="{!URLFOR($Resource.jQuery, '/css/ui-lightness/jquery-ui-1.8.6.custom.css')}"  />-->
    <apex:attribute assignTo="{!recordId}" name="recId" description="Record Id" type="string" required="true"/>
    <apex:attribute name="reportpagename" type="String" description="pagename"/>
    <apex:attribute name="reRenderFeedbackCompId" description="HTML id of the component that needs to be rerendered to refresh the report" type="string" required="true"/>
     <apex:attribute assignTo="{!reportComponentUniqueName}" name="reportCompUniqName" description="Unique Name form the metadata" type="string"/>
    <!-- <apex:attribute name="reportCompUniqName" description="Unique Name form the metadata" type="string"/>-->
      

   <style>
 #main{
  position:absolute;
  padding:3px;
 
}
//body.light1 #textareaid{float:left;margin-left:-26px;margin-top:-5px;}
//body #textareaid{float:left;margin-left:-26px;margin-top:-5px;}
//body.sf1 #commentid{cursor:pointer;margin-left:-34px;float:left;}
 //body.sf1 #textareaid{}

#count_display{
display:block;
font-size:x-small;
}
</style>

    <script type="text/javascript"> 

function likeFunction(param1) {
 
     //alert('hi'+param1);
     likeDislike(param1);
  
}
    
   function Savefunction(param2) {
   //alert('hi'+param2);
    savecomment(param2);
 }
   </script>
     <apex:form id="formid">
         <apex:actionFunction name="likeDislike"  action="{!likeDislikemethod}" rerender="{!reRenderFeedbackCompId}">
         <apex:param name="param1" assignTo="{!param1}" value=""/>
        </apex:actionFunction>
        
         <apex:actionFunction name="savecomment"  action="{!Save}" rerender="{!reRenderFeedbackCompId}">
         <apex:param name="param2" assignTo="{!param2}" value="" />
        </apex:actionFunction>
        
   
       <div style="margin-top:20px;" id="main">
           
        <div style="float:left;width:35px;height:40px;margin-top:-9px;background-color:black;">
            
          
             <a href="">
                 <img src="{!URLFOR($Resource.feedbackIcon,'feedback_ifsvg.svg')}" title="Feedback " style="cursor:pointer;position:absolute;padding:4px;margin-top:2px;margin-left:2px;" width="35" height="35" alt="Help" />
             </a>
             
           </div>
           <div id="textareaid" style="float:left;margin-left:-26px;margin-top:-4px;">
               
                  <apex:inputtextarea value="{!buttontext}" rows="1" cols="35" id="textid" html-placeholder="Please enter feedback..."  style="margin-left: 30px;" />                                            
          </div>
          
           <a href="">
                 <img src="{!URLFOR($Resource.feedbackIcon,'CommentSVG.svg')}" title="Post Feedback" style="cursor:pointer;margin-left:3px;float:left;margin-top:-4px;" width="28" height="28" alt="Help" id="commentid" onclick="Savefunction('{!reportComponentUniqueName}-Comment')"/> 
                 
            </a>
             <div id="L_count" style="float:left;margin-top:-4px;">
           <a href="">
              <img src="{!URLFOR($Resource.feedbackIcon,'ThumbsUpPNG.png')}" title="Like" style="cursor:pointer;margin-left:3px;" width="25" height="25" alt="Help" id="Likeid" onclick="likeFunction('{!reportComponentUniqueName}-Like')" 
                   rendered = "FALSE"/>  
           </a>
          <apex:outputText value="{!LikeCount}" style="font-size:x-small;"/>
         </div>
        <div id="D_count" style="float:left;margin-left:2px;margin-top:-4px;">
          <a href="">  
              <img src="{!URLFOR($Resource.feedbackIcon,'ThumbsDownSVG.svg')}" title="Dislike" style="cursor:pointer;" width="25" height="25" alt="Help" id="Dislikeid" onclick="likeFunction('{!reportComponentUniqueName}-Dislike')" 
                   rendered = "FALSE"/>
            </a>
         <apex:outputText value="{!DislikeCount}" style="font-size:x-small;"/>
         </div>
        
            <!--
            <a href="">
                 <img src="{!URLFOR($Resource.feedbackIcon,'ProblemSVG.svg')}" title="Problem" style="cursor:pointer;" width="25" height="25" alt="Help" id="problemid" onclick="Savefunction('{!reportComponentUniqueName}-Problem')"/>
             </a>
             <a href="">
                 <img src="{!URLFOR($Resource.feedbackIcon,'help.png')}" title="Help" style="cursor:pointer;" width="25" height="25" alt="Help"/>
              
              </a>
          -->
         </div>
           
          
     
        
     
       </apex:form>
    </apex:component>

VF Page:



<apex:page showHeader="false" standardStylesheets="false" sidebar="false" applyHtmlTag="false" applyBodyTag="false" docType="html-5.0" lightningStylesheets="true" standardController="Account">
    <html xmlns="http://www/w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" lang="en">
        <head>
            <meta charset="utf-8"/>
            <meta http-equiv="x-us-compatible" content="ie=edge" />
            <meta name="viewport" content="width=device-width, initial-scale=1" /> 
            <!-- Import the Design System style sheet-->
            <apex:slds />
        </head>
        <body >
            <!-- REQUIRED SLDS WRAPPER -->
            <div class="slds-scope">
                <div class="slds-grid slds-wrap">
                    <div class="slds-col slds-size_1-of-1">
                        <apex:outputPanel id="aoptPanel">
                            <c:SLDS_ReportChartWFilters reportCompUniqName="AOPT" recId="{!Account.Id}" reRenderCompId="aoptPanel" reportPageName="AccountOrganizationPercentileTrend"/>
                        </apex:outputPanel>
                    </div>
                </div>
            </div>
            <div class="slds-col slds-size_1-of-1 slds-small-size_1-of-1 slds-medium-size_1-of-2 slds-large-size_1-of-2">
                <apex:outputPanel layout="block" styleClass="chart-panel slds-scrollable" id="feedbackpane6">
                    <c:TrendingFeedbackIcon reportCompUniqName="Revenue Percentile Ranking" recId="{!Account.Id}" reRenderFeedbackCompId="feedbackpane6" reportPageName="Revenue Percentile Ranking"/>
                </apex:outputPanel>
            </div>
        </body>
    </html>
</apex:page>
                              

likedislike method is failing due to param1 . how to pass param1 and param2 values.



Regards,
Isha