function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ker DeveloperKer Developer 

Problem with Test Class for controller

Hello , 

 

I have a detail page of a cutom object named "Projet__c" .In the controller I 'am using 

System.currentPagereference().getParameters().get('id');

 In the Test Class am Using .Put(ID) but i can't Cover Lines Here is the Class controller : 

public without sharing class MADEL_DetailProjet 
{
    public Projet__c projet {get;set;}
    public List<Article__c> lstarticles{get;set;}
    public List<Equipedeprojet__c> lstequipesprojets{get;set;}

    public Map<Id,MADEL_Contact__c> mapContent  {set;get;}
    public List <Id> contactIds =new List<Id>();
    public List <MADEL_Contact__c > contactsMADEL =new List<MADEL_Contact__c>();
    public string projectid{get;set;}
    
    
     
    MADEL_Contact__c contact{get;set;}
    

    public MADEL_DetailProjet() 
    {
        
    try{
    projectid = System.currentPagereference().getParameters().get('id');
       if(projectid!=null)
       { projet = [SELECT id,Name,Datededebut__c,Datedepublication__c,Dureeestime__c,Thematique__c,SousThematique__c,Rue__c,Ville__c,Region__c,Codepostal__c,Pays__c,Longitude__c,Latitude__c   FROM Projet__c WHERE id = :projectid LIMIT 1];
        
        lstarticles=[select Id ,Name,Type__c,Lien__c,ProfessionnelduMedia__c From Article__c where Projet__c= :projectid];
        
        lstequipesprojets=[select Id ,Name,Membre__c,Projet__r.Name,Role__c,MembreMADEL__r.Name,MembreMADEL__r.GSM1__c From Equipedeprojet__c where Projet__c= :projectid];
       
       if(lstequipesprojets.size()>0)
       {
        for(Equipedeprojet__c equipe:lstequipesprojets)
        {
          contactIds.add(equipe.Membre__c);
  
        }
       }
       }
         contactsMADEL=[select Id, Name,GSM1__c,Idcontactstd__c  from MADEL_Contact__c where Idcontactstd__c IN : contactIds ];
             Map<Id,MADEL_Contact__c> mapResult = new Map<Id,MADEL_Contact__c>();
         
         for(MADEL_Contact__c contactmadel:contactsMADEL)
        {
        
        mapResult.put(contactmadel.Id,contactmadel);
        
     
        }
        mapContent=mapResult;
        
       
        

}

 catch (DMLException e){
     ApexPages.addMessages(e);
     system.debug('messge erreur'+e);
     }
    
    
    
    }


 
 
 
   
    
    
    
    



}

 And Test Class 

@isTest
private class Test_MADELDetailProjet {

    public static testMethod void detailProjetTest() 
    {
    	 Projet__c projet  = new Projet__c (Name='projet2012');
      insert projet;
       string projetid= projet.Id;
	  
    	
      Test.startTest();
      PageReference newpage = Page.MADEL_DetailProjet;
      
     
      MADEL_DetailProjet cls = new MADEL_DetailProjet();
   
        
       newpage.getParameters().put('id',projet.Id);
       cls.projectid=projet.Id;
      Test.setCurrentPage(newpage);
      Test.setCurrentPageReference(newpage);
      
  
    	
   
    	Test.stopTest();
       
       
       
       
       
       
       
       
    }
}

 

 

 And Here is an image of line not covered 

 

 

 

HariDineshHariDinesh

Hi,

 

In the Main class change the bow line like this

if(projectid!=null)---------->  if(projectid != '')

 

This will solve the problem.

 

If even this doesn't solve the problem use Debug statement's to find whether is there any problem with the insert statement in test method (whether data is inserting or not).

Ker DeveloperKer Developer

Thank you for your reply . 

 

When i did the modification i have List has no rows for assignement . 

 

I am testing a cutstom controller in my page using Controller instead of StandardController+Extensions.

 

Thanks

HariDineshHariDinesh

Hi,

 

Did it solve the problem or still getting …?

Ker DeveloperKer Developer

Still getting :s , Here i my page : 

 

public without sharing class MADEL_DetailProjet 
{
    public Projet__c projet {get;set;}
    public List<Article__c> lstarticles{get;set;}
    public List<Equipedeprojet__c> lstequipesprojets{get;set;}

    public Map<Id,MADEL_Contact__c> mapContent  {set;get;}
    public List <Id> contactIds =new List<Id>();
    public List <MADEL_Contact__c > contactsMADEL =new List<MADEL_Contact__c>();
    public string projectid{get;set;}
    
    
     
    MADEL_Contact__c contact{get;set;}

    public MADEL_DetailProjet(ApexPages.StandardController controller) {
           
    try{
     
   projet= (Projet__c )controller.getRecord();
    
    projectid = projet.Id;
       if(projectid!=null)
       { projet = [SELECT id,Name,Datededebut__c,Datedepublication__c,Dureeestime__c,Thematique__c,SousThematique__c,Rue__c,Ville__c,Region__c,Codepostal__c,Pays__c,Longitude__c,Latitude__c   FROM Projet__c WHERE id = :projectid LIMIT 1];
        
        lstarticles=[select Id ,Name,Type__c,Lien__c,ProfessionnelduMedia__c From Article__c where Projet__c= :projectid];
        
        lstequipesprojets=[select Id ,Name,Membre__c,Projet__r.Name,Role__c,MembreMADEL__r.Name,MembreMADEL__r.GSM1__c From Equipedeprojet__c where Projet__c= :projectid];
       
       if(lstequipesprojets.size()>0)
       {
        for(Equipedeprojet__c equipe:lstequipesprojets)
        {
          contactIds.add(equipe.Membre__c);
  
        }
       }
       }
         contactsMADEL=[select Id, Name,GSM1__c,Idcontactstd__c  from MADEL_Contact__c where Idcontactstd__c IN : contactIds ];
             Map<Id,MADEL_Contact__c> mapResult = new Map<Id,MADEL_Contact__c>();
         
         for(MADEL_Contact__c contactmadel:contactsMADEL)
        {
        
        mapResult.put(contactmadel.Id,contactmadel);
        
     
        }
        mapContent=mapResult;
        
       
        

}

 catch (DMLException e){
     ApexPages.addMessages(e);
     system.debug('messge erreur'+e);
     }
    

    }

    
    

    public MADEL_DetailProjet() 
    {
 
    
    
    }


 
 
 
   
    
    
    
    



}