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
HARI KRISHNAHARI KRISHNA 

Unit test Coverage Problem

User-added image
--------------------This is the Class--------------
public class task1 {
    public string Obj{get;set;}
    public integer Year{get;set;}   
    public boolean Abool{set;get;}
    public boolean Bbool{set;get;}
    public boolean Cbool{set;get;}
    public boolean Dbool{set;get;}            
    public list<account> acclist{set;get;}
    public list<contact> conlist{set;get;}
    public list<opportunity> opplist{set;get;}
    public list<lead> Lelist{set;get;}             
    
    public task1()
    {
        Abool=false;
        Bbool=false;
        Cbool=false;
        Dbool=false;                        
        acclist=new list<account>();
        conlist=new list<contact>();
        opplist=new list<opportunity>();
        Lelist=new list<lead>();                        
    }
    public void getData()
    {
    if(Obj.equals('None')&&Year==0)
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please ensure to select Object and Year'));
         else
        if(Obj.equals('Account'))
        {
            acclist=[select id,name,phone from Account WHERE CALENDAR_YEAR(CreatedDate) =: Year];
            Abool=true;
        }else
        if(Obj.equals('Contact'))
        {
            conlist=[select id,name,phone from contact WHERE CALENDAR_YEAR(CreatedDate) =: Year];
            Bbool=true;
        }else
        if(Obj.equals('Opportunity'))
        {
            opplist=[select id,name,amount from Opportunity WHERE CALENDAR_YEAR(CreatedDate) =: Year];
            Cbool=true;
        }else
        {
         Lelist=[select id,name,phone from Lead WHERE CALENDAR_YEAR(CreatedDate) =: Year];   
        Dbool=true;
        }        
    }
}
---------------------This is the Test Class which is able to cover only 63%-------------------Help me to resolve the issue-----------------
@isTest
public class  Task1_TC
{
    static testMethod void testMthd()
    {
        task1 t=new task1();
        string Obj='Opportunity';
        Account a=new Account(name='xxx');
         Lead l=new Lead(lastname='yyy');
         Contact c=new Contact(lastname='kkk');
         Opportunity o=new opportunity(name='OOO');
         integer Year=2014;
         boolean Abool=false;
         boolean Bbool=false;
         boolean Cbool=false;
         boolean Dbool=false;
         list<account> acclist=new list<account>();
         acclist.add(a);
         list<contact> conlist=new list<contact>();
         conlist.add(c);
         list<opportunity> opplist=new list<opportunity>();
         opplist.add(o);
         list<lead> Lelist=new list<lead>();
         lelist.add(l);
         t.getData();         
    }
}
PratikPratik (Salesforce Developers) 

Hi Hari,

As per the screenshot , i can see your conditional statements If are not covered. Please try to set the values with both I & else criteria.

You can go through the guide:
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_best_practices.htm

Thanks,
Pratik

HARI KRISHNAHARI KRISHNA
User-added image
------------------Modified Test Class-----------------------It s able to cover 78% almost all line of code-----but not all---help me how to cover ApexPages line----and remaining----------------
@isTest
public class  Task1_TC
{
    static testMethod void testMthd()
    {
        task1 t=new task1();
        t.Obj='';
        Account a=new Account(name='xxx');
         Lead l=new Lead(lastname='yyy');
         Contact c=new Contact(lastname='kkk');
         Opportunity o=new opportunity(name='OOO');
         integer Year=2014;
          t.Abool=false;
          t.Bbool=false;
          t.Cbool=false;
          t.Dbool=false;
         list<account> acclist=new list<account>();
         acclist.add(a);
         list<contact> conlist=new list<contact>();
         conlist.add(c);
         list<opportunity> opplist=new list<opportunity>();
         opplist.add(o);
         list<lead> Lelist=new list<lead>();
         lelist.add(l);
         t.getData();         
    }
}