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
Jasveer SinghJasveer Singh 

Help to achieve 100% code coverage

This is my Both classes And code coverage is 86% please help me to achieve 100% code coverage
Thanks & Regards
Jasveer Singh

public class WrapperClearFunda {

public List<Account> acclist{get;set;}
public List<Contact> conlist{get;set;}
public List<Wrapperclass> wl{get;set;}

public List<Wrapperclass> wlacc{get;set;}
public List<Wrapperclass> wlcon{get;set;}

public WrapperClearFunda(){
acclist=new List<Account>();
conlist=new List<Contact>();

wlacc=new List<Wrapperclass>();
wlcon=new List<Wrapperclass>();

wl=new List<Wrapperclass>();
acclist=[select name from account];
conlist=[select Lastname from Contact];

// insert value into 1st Constructor
for(Integer i=0;i<5;i++)
{
wl.add(new Wrapperclass(acclist[i],conlist[i]));
}

// insert value into 2nd Constructor
for(Account a:acclist){
wlacc.add(new Wrapperclass(a));
}


// insert value into 3rd Constructor
for(Contact c:conlist){
wlcon.add(new Wrapperclass(c));
}


}



public class Wrapperclass{
public Account acc{get;set;}
public Contact con{get;set;}

// 1st Constructor
public Wrapperclass( Account acc, Contact con){
this.acc=acc;
this.con=con;
}

// 2nd Constructor
public Wrapperclass( Account acc){
this.acc=acc;
}

// 3rd Constructor
public Wrapperclass( Contact con){
this.con=con;
}

}


}


+++++++++++++++++++++++++++++++++++++++

@isTest 
private class WrapperClearFundaTest{
  // WrapperClearFunda wcf=new WrapperClearFunda ();
  
    static testMethod void validateHelloWorld1() {
    Account acc=new Account();
    Contact con=new Contact();
    WrapperClearFunda.Wrapperclass w1=new WrapperClearFunda.Wrapperclass(acc,con);
    }
    
    static testMethod void validateHelloWorld2() {
    Account acc=new Account();  
    WrapperClearFunda.Wrapperclass w2=new WrapperClearFunda.Wrapperclass(acc);
    }


    static testMethod void validateHelloWorld3() {
    Contact con=new Contact();
    WrapperClearFunda.Wrapperclass w3=new WrapperClearFunda.Wrapperclass(con);
    }


    static testMethod void validateHelloWorld4() {
    WrapperClearFunda wcf=new WrapperClearFunda ();
   // WrapperClearFunda.Wrapperclass w4=new WrapperClearFunda.Wrapperclass();
    Account acc=new Account(name='axz');
    Contact con=new Contact(lastname='abc');
          insert acc;
          insert con;
          System.assertEquals('abc', con.lastname);
          System.assertEquals('axz', acc.name);
    }

}
 
Best Answer chosen by Jasveer Singh
Rohit K SethiRohit K Sethi
Hi 

Write only single method in test class as below and this will cover your code 100%.
static testMethod void validateHelloWorld4() {
        List<Account> lstAccount = new List<Account>();
        
        lstAccount.add(new Account(name='axz1'));
        lstAccount.add(new Account(name='axz2'));
        lstAccount.add(new Account(name='axz3'));
        lstAccount.add(new Account(name='axz4'));
        lstAccount.add(new Account(name='axz5'));
        lstAccount.add(new Account(name='axz6'));
        
        List<Contact> lstContact = new List<Contact>();
        lstContact.add(new Contact(lastname='abc1'));
        lstContact.add(new Contact(lastname='abc2'));
        lstContact.add(new Contact(lastname='abc3'));
        lstContact.add(new Contact(lastname='abc4'));
        lstContact.add(new Contact(lastname='abc5'));
        lstContact.add(new Contact(lastname='abc6'));
        
        insert lstAccount;
        insert lstContact;
        WrapperClearFunda wcf=new WrapperClearFunda ();
        
    }



If this post solves your problem kindly mark it as solution.
Thanks.

All Answers

Rohit K SethiRohit K Sethi
Hi 

Write only single method in test class as below and this will cover your code 100%.
static testMethod void validateHelloWorld4() {
        List<Account> lstAccount = new List<Account>();
        
        lstAccount.add(new Account(name='axz1'));
        lstAccount.add(new Account(name='axz2'));
        lstAccount.add(new Account(name='axz3'));
        lstAccount.add(new Account(name='axz4'));
        lstAccount.add(new Account(name='axz5'));
        lstAccount.add(new Account(name='axz6'));
        
        List<Contact> lstContact = new List<Contact>();
        lstContact.add(new Contact(lastname='abc1'));
        lstContact.add(new Contact(lastname='abc2'));
        lstContact.add(new Contact(lastname='abc3'));
        lstContact.add(new Contact(lastname='abc4'));
        lstContact.add(new Contact(lastname='abc5'));
        lstContact.add(new Contact(lastname='abc6'));
        
        insert lstAccount;
        insert lstContact;
        WrapperClearFunda wcf=new WrapperClearFunda ();
        
    }



If this post solves your problem kindly mark it as solution.
Thanks.
This was selected as the best answer
Jasveer SinghJasveer Singh
Thanks Rohit

can you help me this code too.............


public class accToPick 
{
   
List<Account> acc;
public String val{get;set;}
 public List<cContact> ist{get;set;}
  public String index {get;set;}
  
  public String fname{get;set;}
  public String lname{get;set;}
  

    public accToPick()
    {
    acc= [SELECT id,name FROM Account];
    
    
    
    }
    
        
 
    
    public List<SelectOption> getAccounnames() 
    {
        List<SelectOption> accOptions= new List<SelectOption>();
        for(Account acc2:acc)
            {
            
            accOptions.add(new selectOption(acc2.id,acc2.name));
            }
        return accOptions;
    }
    

    public List<cContact> conload()
    {    
          ist= new List<cContact>();
        for(Contact a:[SELECT id,name,email from Contact where Account.Id=:val])
        {
            ist.add(new cContact(a));
        }
        
         return null;
    }
    
    public void addContact()
        {
        Contact lc=new Contact(FirstName=fname,LastName=lname,AccountId=val);
        
            try
            {
                insert lc;
            }
            
            catch(Exception e)
            {
                ApexPages.addMessages(e);
            }
            conload();
            fname='';
            lname='';
        }
     
     public void deletecon()
     {       
         Contact c1=[select id from Contact where id=:index];
         delete c1;   
         conload();     
     }
     
     public PageReference sendmail()
     {
        
         List<Contact> selectedContacts = new List<Contact>();
        //System.debug(selected + 'abc');
        for(cContact cCon :ist) 
        {
        
            if(cCon.selected==true) 
            {
                selectedContacts.add(cCon.con);
                
            }
            
        }
        System.debug('These are the selected Contacts…');
        for(Contact con : selectedContacts) 
        {
            string conEmail = con.Email;
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] {conEmail};
            mail.setToAddresses(toAddresses);
            mail.setReplyTo('huria.99.anuj@gmail.com');
            mail.setSenderDisplayName('Salesforce Support');
            mail.setSubject('New Case Created : ' + case.Id);
            mail.setBccSender(false);
            mail.setUseSignature(false);
            mail.setPlainTextBody('Thank for Contacting');
            mail.setHtmlBody('Thank for Contacting');
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }
        return null;
     }
     
      public class cContact 
      {
        public Contact con {get; set;}
        public Boolean selected {get; set;}
        public cContact(Contact c)
        {
            con = c;
            selected = false;
        }
    }
    
    
}

+++++++++++++++++++++++++++++++++++++++


@isTest 
private class accToPickTest{
 
  
    static testMethod void validateHelloWorld1() {
   Contact con=new Contact();
    accToPick.cContact  w1=new   accToPick.cContact(con);
    }
    
     static testMethod void validateHelloWorld1_1() {
    accToPick atp=new accToPick ();
    atp.getAccounnames();

    }
    
      static testMethod void validateHelloWorld1_2() {
    accToPick atp=new accToPick ();
    atp.conload();
    }
    
      static testMethod void validateHelloWorld2() {
  accToPick atp=new accToPick ();
  atp.addContact();
    }
  
  static testMethod void validateHelloWorld3() {
  accToPick atp=new accToPick ();
  atp.deletecon();
  
    }
    
    static testMethod void validateHelloWorld4() {
  accToPick atp=new accToPick ();
  atp.sendmail();
    }
 
}



// code coverage is 58% 
Rohit K SethiRohit K Sethi
Hi jasveer singh,

Write this sing  function in your test class this will cover your code.
 
public static testMethod void unitTest1(){
        List<Account> lstAccount = new List<Account>();
        lstAccount.add(new Account(name='axz1'));
        lstAccount.add(new Account(name='axz2'));
        lstAccount.add(new Account(name='axz3'));
        lstAccount.add(new Account(name='axz4'));
        lstAccount.add(new Account(name='axz5'));
        lstAccount.add(new Account(name='axz6'));
        insert lstAccount;
                
        List<Contact> lstContact = new List<Contact>();
        lstContact.add(new Contact(lastname='abc1',email='test2@gmail.com'));
        lstContact.add(new Contact(lastname='abc2',email='test1@gmail.com'));
        lstContact.add(new Contact(lastname='abc3',accountId=lstAccount[1].id,email='test2@gmail.com'));
        lstContact.add(new Contact(lastname='abc4',accountId=lstAccount[0].id,email='test3@gmail.com'));
        lstContact.add(new Contact(lastname='abc5',accountId=lstAccount[0].id,email='test4@gmail.com'));
        lstContact.add(new Contact(lastname='abc6',accountId=lstAccount[0].id ,email='test6@gmail.com'));
        

        insert lstContact;
        
        accToPick pick = new accToPick ();
        pick.getAccounnames();
        pick.conload();
        pick.fName ='testFirst';
        pick.lNAme= 'testLastNAme';
        pick.val = lstAccount[0].id;
        pick.addContact();
        pick.index = lstContact[1].id;
        pick.deletecon();
        
        pick.ist[0].selected = true;
        pick.ist[1].selected = true;
        pick.sendmail();
        
  }

Thanks.