+ Start a Discussion
fadwa mangougfadwa mangoug 

Testing a visualforce page

hey there,
I've just stated using salesforce and apex and I'd like to have some help with my testing methods. If anyone can helpl plz i'd be gratefull !
Here's the visualforce page code  :

<apex:page controller="progress_bar">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
   <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
 <apex:pageblock title="Feedbacks by Severity">
        <form>     
      <center>
             <br/> <br/> <br/> <br/>
                <div class="progress">
                    <div class="progress-bar progress-bar-striped progress-bar-success" role="progressbar" style="width:35%">
                        {!Normal} Normal
                    </div>
                   
                    <div class="progress-bar progress-bar-striped active progress-bar-warning" role="progressbar" style="width:20%">
                        {!Blocking} Blocking
                    </div>
                   
                    <div class="progress-bar progress-bar-striped progress-bar-danger" role="progressbar" style="width:20%">
                        {!Urgent} Urgent
                    </div>
                 </div>
          </center>
  </form>
     <style>
            .progress {
            color : white;
            font-weight : bold;
            font-size : 100%;
           
            }
     </style>
 </apex:pageblock>
</apex:page>


and the apex class used :

public class progress_statut {

    public integer getNew(){
        return [select count() from feedbacks__c where Feedback_statut__c='New'];
    }
  
 public integer getAssigned(){
        return [select count() from feedbacks__c where Feedback_statut__c='Assigned'];
    }
    public integer getAssignedEditor(){
        return [select count() from feedbacks__c where Feedback_statut__c='Assigned to Editor'];
    }
 public integer getInformationNeeded(){
        return [select count() from feedbacks__c where Feedback_statut__c='Information Needed'];
    }
 
    public integer getEffortEstimation(){
        return [select count() from feedbacks__c where Feedback_statut__c='Effort Estimation'];
    }
     public integer getwillnotfix(){
        return [select count() from feedbacks__c where Feedback_statut__c='Will not fix'];
    }
    public integer getresolved(){
        return [select count() from feedbacks__c where Feedback_statut__c='Resolved'];
    }
    public integer getdelivered(){
        return [select count() from feedbacks__c where Feedback_statut__c='Delivered'];
    }
    public integer getclosed(){
        return [select count() from feedbacks__c where Feedback_statut__c='Closed'];
    }
}


Thanks for the help in advance !!


 
Best Answer chosen by fadwa mangoug
Mustafa JhabuawalaMustafa Jhabuawala
You simply need to write test class for your controller class. Ref this - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm

Then in test class create a method and then create object of your controller class.
progress_statut objName = new progress_statut();

Now you need to call the merhods present in the controller class i.e getNew, getAssigned and store their output in some related datatypes.
Integer someData = objName.getName();
To achieve code coverage this is OK. But recommended way is to also check whether the output is accurate or not using asserts. In your case its simpler. For ex:
 
System.assertEquals(someData,select count() from feedbacks__c where Feedback_statut__c='New');
So this will compare the count returned by your SOQL and by your method.

In case you find this helpfull. Please mark this as a best answer to help others.

Thanks..



 

All Answers

Mustafa JhabuawalaMustafa Jhabuawala
You simply need to write test class for your controller class. Ref this - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm

Then in test class create a method and then create object of your controller class.
progress_statut objName = new progress_statut();

Now you need to call the merhods present in the controller class i.e getNew, getAssigned and store their output in some related datatypes.
Integer someData = objName.getName();
To achieve code coverage this is OK. But recommended way is to also check whether the output is accurate or not using asserts. In your case its simpler. For ex:
 
System.assertEquals(someData,select count() from feedbacks__c where Feedback_statut__c='New');
So this will compare the count returned by your SOQL and by your method.

In case you find this helpfull. Please mark this as a best answer to help others.

Thanks..



 
This was selected as the best answer
fadwa mangougfadwa mangoug
Thanks a lot for the help and the quick answer.
 
fadwa mangougfadwa mangoug
Hey again,
I have another question concerning the tests but this time its about test methods for classes using a standard controller; I managed to write this part of the class test for now and it only gives me 60% code coverage. Do you plz have any idea to improve it.

Apex class :

public class feedbackStatuts {
    public String feedback_status {get; set;}
    private String status;
   
      
  
    public feedbackStatuts(ApexPages.StandardController stdController) {
         
         //Initialisation de variables
         feedback_status = '';
         status = ApexPages.currentPage().getParameters().get('Feedback_statut__c');   
         
         if (status=='New'){
             status='Assigned';
   } else if (status=='Assigned'){
             status='Assigned to editor';
            } else if (status=='Assigned to editor'){
             status='Information Needed';
            } else if (status=='Information needed'){
             status='Effort estimation';
            } else if (status=='Effort estimation'){
             status='Will not fix';
            } else if (status=='Will not fix'){
             status='Resolved';
            }else if (status=='Resolved'){
             status='Delivered';
            }else if (status=='Delivered'){
             status='Closed';
            }
         
          
     }
   
   
   
}


and the test class that 've written for now is like :

@isTest
public class feedbackStatutsTest {
   
    static testMethod void feedbackStatutsTest() {
       
        Feedbacks__c f = new Feedbacks__c(Name = 'trial',Feedback_statut__c='New');
        System.debug('Feedback status before change ' + f.Feedback_statut__c);
        Insert f;
       
   Test.StartTest();
        ApexPages.StandardController sc = new ApexPages.StandardController(f);
        feedbackStatuts FB = new feedbackStatuts(sc);
       
       
  PageReference pageRef = Page.change_status;
        pageRef.getParameters().put('Id',f.id);
     Test.setCurrentPage(pageRef);
      

        Test.StopTest();

    }
}


Thanks for the help :) !
Mustafa JhabuawalaMustafa Jhabuawala
There are multiple if conditions and your test code doesn't reach to each and every line so the coverage is less. 

You have to write code which sets parameter as per your different if conditions i.e New, Assigned,  Assigned to editor etc.
ApexPages.currentPage().getParameters().put('Feedback_statut__c','Assigned');
So for every condition write above line to set the different parameters value. And hence you can achieve full coverage.
 
fadwa mangougfadwa mangoug
thanks a lot for both answers, very helpfull.
So Gratefull :)
Mustafa JhabuawalaMustafa Jhabuawala
my pleasure :)