+ Start a Discussion
Ker DeveloperKer Developer 

problem with test class for custom controller

Hello , 

 

I used to test extensions with no problem , now i want to test custom controller but i have problem with coverage . 

 

My page is just viewing detail of an object (evenement) 

 

Here is my page : 

 

<apex:page showHeader="false" title="Madel - Agenda" cache="false" controller="MADEL_Ctrl005_DetailsEvenement">
 

                                        <ul>
                                          <li><strong>Evènement </strong> : <br/><br/>&nbsp;&nbsp;&nbsp;<apex:outputText value="{!e.Name}"></apex:outputText></li>                                         
                                          <li><strong>Date début</strong> : <br/><br/>&nbsp;&nbsp;&nbsp;<apex:outputText value="{!e.Datedebut__c}"></apex:outputText></li>
                                          <li><strong>Date fin</strong> : <br/><br/>&nbsp;&nbsp;&nbsp;<apex:outputText value="{!e.Datefin__c}"></apex:outputText></li>
                                          <li><strong>Description</strong> : <br/><br/>&nbsp;&nbsp;&nbsp;<apex:outputText value="{!e.Description__c}"></apex:outputText></li>
                                          <li><strong>Lieu</strong> : <br/><br/>&nbsp;&nbsp;&nbsp;<apex:outputText value="{!e.Lieu__c}"></apex:outputText></li>
                                          
                                        </ul> 


</apex:page>

 And here is my controller ; 

 

public with sharing class MADEL_Ctrl005_DetailsEvenement
{

            
        public evenement__c e  {set;get;}

        
        
        public MADEL_Ctrl005_DetailsEvenement() {
        
              string eventId = System.currentPagereference().getParameters().get('id'); 
         
         if(eventId!=null)
         {
               e =  [ select id,Datedebut__c,Description__c,Datefin__c,evenementpublie__c,Name,Lieu__c from evenement__c
                           where id=:eventId LIMIT 1];

         }   
        
                
        }



}

 My test is : 

 

@isTest
private class Test_MADEL_Ctrl005_DetailsEvenement 
{

    static testMethod void myUnitTest() 
    {
        evenement__c event  = new evenement__c (Name='event',Datedebut__c=system.now());
      insert event;
	  
    	
      Test.startTest();
      PageReference newpage = Page.MadelDetailEvenement;
     
      MADEL_Ctrl005_DetailsEvenement cls = new MADEL_Ctrl005_DetailsEvenement();
    
	  newpage.getParameters().put('id',event.Id);
    
      Test.setCurrentPage(newpage);
      Test.setCurrentPageReference(newpage);
    }
}

 When i run test this line is not covered : 

e =  [ select id,Datedebut__c,Description__c,Datefin__c,evenementpublie__c,Name,Lieu__c from evenement__c
                           where id=:eventId LIMIT 1];

 

which means that the getparameters.put is not passing the id... i tried days and days but i don't now what is the preoblem . 

 

Please help meee!!

bob_buzzardbob_buzzard

The problem is that you construct the controller and then set the URL parameter, but it is the constructor that extracts the id from the URL.

 

Change your test class as follows:

 

Test.startTest();
PageReference newpage = Page.MadelDetailEvenement;
Test.setCurrentPage(newpage);      Test.setCurrentPageReference(newpage);
newpage.getParameters().put('id',event.Id);
     
MADEL_Ctrl005_DetailsEvenement cls = new MADEL_Ctrl005_DetailsEvenement();