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
sachitanand kumarsachitanand kumar 

hi.. how i can got 75% code coverge in following apex class.currently i m able to cover only 68 % any body can help me

here is my apex class
public class wrapperexample2 {
    public wrapperexample2(ApexPages.StandardSetController controller) {        
    }
public List<Schema.contact> SList{get;set;}  //this line was not cover in test class
List<wrapper> WList=New List<wrapper>();
public List<wrapper> getLstwrapperstring()    //this line was not cover in test class
    {
        slist=[SELECT Account_name__c,Name,Phone,Title,Email FROM Contact WHERE AccountId =:ApexPages.CurrentPage().GetParameters().Get('id')];  //this line was not cover in test class
        for(Integer i=0 ;i<SList.size();i++){          //this line was not cover in test class
           WList.add(New wrapper(SList[i].name,SList[i].Account_name__c,SList[i].phone,SList[i].Title,SList[i].Email)); //this line was not cover in test class
        }
        return WList;   //this line was not cover in test class
    }
 public class wrapper
 {
     public string cid{get;set;}
     public string cName{get;set;}
     public string cPhone{get;set;}
     public string cTitle{get;set;}
      public string cEmail{get;set;}
   public wrapper(string cName,string cid,string cPhone,string cTitle,string cEmail)
     {
         this.cid=cid;
         this.cName=cName;
         this.cPhone=cPhone;
         this.cTitle=cTitle;
         this.cEmail=cEmail;
     }  
 }
}

and here is my current test class
@isTest
public class contact_pdf_test {
    
    static testMethod void data1(){
      List<contact> acc = new List<contact>();
        contact a = New contact();
        a.firstName = 'mycontact';
        a.lastName = 'mycontact';
        a.phone='124365';
        a.Title='Customer';
        a.Email ='test@gmail.com';
        acc.add(a);
        insert acc; 
              
 ApexPages.StandardSetController  sac = new ApexPages.StandardSetController (acc);  
 wrapperexample2 aop = new wrapperexample2(sac);   
     test.startTest();
        PageReference myVfPage = Page.contact_pdf;
         Test.setCurrentPage(myVfPage);
        String mycontact;
        String cid;
        String cPhone;
        String cTitle;
        String cEmail;
        wrapperexample2.wrapper sacc = new wrapperexample2.wrapper(mycontact,cid,cPhone,cTitle,cEmail);                    
     test.stopTest();
    }
 }

 
Best Answer chosen by sachitanand kumar
Varun SinghVarun Singh
Hi 
You will get 100% covearge from this code please  update values of list and also call your wrapper method;

try this code
 
​@isTest
public class contact_pdf_test {
    
    static testMethod void data1(){
      List<contact> acc = new List<contact>();
        contact a = New contact();
        a.firstName = 'mycontact';
        a.lastName = 'mycontact';
        a.phone='124365';
        a.Title='Customer';
        a.Email ='test@gmail.com';
        acc.add(a);
        insert acc; 
              
 ApexPages.StandardSetController  sac = new ApexPages.StandardSetController (acc);  
 wrapperexample2 aop = new wrapperexample2(sac);   

aop.SList=acc;
 aop.getLstwrapperstring();
     test.startTest();
        PageReference myVfPage = Page.contact_pdf;
         Test.setCurrentPage(myVfPage);
        String mycontact;
        String cid;
        String cPhone;
        String cTitle;
        String cEmail;
        wrapperexample2.wrapper sacc = new wrapperexample2.wrapper(mycontact,cid,cPhone,cTitle,cEmail);                    
     test.stopTest();
    }
 }

If Information is informative please select my answer as best answer

All Answers

Nayana KNayana K
@isTest
public class contact_pdf_test 
{
    
    static testMethod void data1()
	{
		Account objAcc = new Account(Name = 'Acc');
		insert objAcc;
		
		List<contact> lstCon = new List<contact>();
        contact a = New contact(AccountId = objAcc.Id);
        a.firstName = 'mycontact';
        a.lastName = 'mycontact';
        a.phone='124365';
        a.Title='Customer';
        a.Email ='test@gmail.com';
        lstCon.add(a);
        insert lstCon; 
        
		PageReference myVfPage = Page.contact_pdf;
		Test.setCurrentPage(myVfPage);
		ApexPages.currentPage().getParameters().put('id', objAcc.Id);		
		ApexPages.StandardSetController  sac = new ApexPages.StandardSetController (objAcc);  
		wrapperexample2 aop = new wrapperexample2(sac);   
		test.startTest();
        
        List<wrapperexample2.wrapper> lstWrap = aop.getLstwrapperstring();     
		system.assertEquals(1, lstWrap.size());	
		test.stopTest();
    }
 }

 
Varun SinghVarun Singh
Hi 
You will get 100% covearge from this code please  update values of list and also call your wrapper method;

try this code
 
​@isTest
public class contact_pdf_test {
    
    static testMethod void data1(){
      List<contact> acc = new List<contact>();
        contact a = New contact();
        a.firstName = 'mycontact';
        a.lastName = 'mycontact';
        a.phone='124365';
        a.Title='Customer';
        a.Email ='test@gmail.com';
        acc.add(a);
        insert acc; 
              
 ApexPages.StandardSetController  sac = new ApexPages.StandardSetController (acc);  
 wrapperexample2 aop = new wrapperexample2(sac);   

aop.SList=acc;
 aop.getLstwrapperstring();
     test.startTest();
        PageReference myVfPage = Page.contact_pdf;
         Test.setCurrentPage(myVfPage);
        String mycontact;
        String cid;
        String cPhone;
        String cTitle;
        String cEmail;
        wrapperexample2.wrapper sacc = new wrapperexample2.wrapper(mycontact,cid,cPhone,cTitle,cEmail);                    
     test.stopTest();
    }
 }

If Information is informative please select my answer as best answer
This was selected as the best answer
sachitanand kumarsachitanand kumar
hi Varun Singh.
its realy work good. thanks for help