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
LoguLogu 

How to increase test class code coverage to 100%

Hi All,
 Can any one help me to increase my test class code coverage to 100%, This is my first test class, Now my test class coverage is 87%, I want to increase the code coverage to 100%, What are the changes i need to do in my test class to get code coverage of 100%?

Thanks in advance.

my controller class
================
public with sharing class AccountListViewTaskCon {
     public Id accountId                                {    get; set;    }
     public Boolean pbTablebool                         {    get; set;    } // boolean value to render pageblocktable
     public String SearchAccName                        {    get; set;    }
     public String queryString                          {    get; set;    }
     List <Account> accounts;
     public Apexpages.StandardSetController controller    {get; set;}
    
     public AccountListViewTaskCon()
     {
        controller    = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Name,Industry,Rating,Account_Priority__c,Owner.Name,Account_Region__c FROM Account]));
        controller.setPageSize(20);
        pbTablebool  = True;           
     }
    public List<Account> getAccounts()
    {
        return (List<Account>) controller.getRecords();
    }
    public void doDelete()
    {
       try
        {
            delete [select Id from Account where Id =: accountId];
        }
        catch(Exception e)
        {
               ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, e.getMessage()));
        }//try
    }
    public void doSearch()
    {
        try{
            //null or empty check
            System.debug('SearchAccName : '+SearchAccName);
            queryString = 'select id,Name,Industry,Rating,Account_Priority__c,Owner.Name,Account_Region__c from Account  where Name Like \''+SearchAccName+'%\' ';
            controller    = new ApexPages.StandardSetController(Database.Query(queryString ));
          }
        catch(System.QueryException qe)// exception handling
        {
          ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.error, qe.getMessage());
          ApexPages.addMessage(msg);
        }
    }
    // indicates whether there are more records after the current page set.
    public Boolean hasNext
    {
        get {
            return controller.getHasNext();
        }
        set;
    }

    // indicates whether there are more records before the current page set.
    public Boolean hasPrevious {
        get {
            return controller.getHasPrevious();
        }
        set;
    }
    // returns the page number of the current page set
    public Integer pageNumber {
        get {
            return controller.getPageNumber();
        }
        set;
    }
    // returns the first page of records
     public void first() {
         controller.first();
     }

     // returns the last page of records
     public void last() {
         controller.last();
     }

     // returns the previous page of records
     public void previous() {
         controller.previous();
     }

     // returns the next page of records
     public void next() {
         controller.next();
     }

     // returns the PageReference of the original page, if known, or the home page.
     public void cancel() {
         controller.cancel();
     }
}

My test class
==========

@isTest(SeeAllData=true)

public class TestAccountListViewTaskCon
{
        public static boolean returnValueBooleanhasNext ;
        
        public static boolean returnValueBooleanhasPrevious  ;
        public static integer pageNumber ;
       
        public static  AccountListViewTaskCon objAccountListViewTaskConPageCon ; 
        static testMethod void runPositiveTestCases() 
        {
             objAccountListViewTaskConPageCon =  new AccountListViewTaskCon(); 
            system.Test.startTest();
            objAccountListViewTaskConPageCon.getAccounts();
            objAccountListViewTaskConPageCon.doDelete();
            objAccountListViewTaskConPageCon.doSearch();
            objAccountListViewTaskConPageCon.first();
            objAccountListViewTaskConPageCon.last();
            objAccountListViewTaskConPageCon.previous();
            objAccountListViewTaskConPageCon.next();
            objAccountListViewTaskConPageCon.cancel();
            returnValueBooleanhasNext  = objAccountListViewTaskConPageCon.hasNext;
            returnValueBooleanhasPrevious  = objAccountListViewTaskConPageCon.hasPrevious; 
             pageNumber =  objAccountListViewTaskConPageCon.pageNumber ;
            System.assertEquals(false, returnValueBooleanhasNext  );
            System.assertEquals(false, returnValueBooleanhasPrevious  );
System.assertEquals(1, pageNumber );
system.Test.stopTest();

        }
      
}
ManojjenaManojjena
Hi Logesh,

I think your catch block is not covered here ,To cover the catch block you have to generate exception in your test code .In your test class there is no such test record you have created . 

There are two catch bock one is for Query exception and another is for DML excption .I have one doubt ,how the account  accoutId variable will get value ?


 
MellowRenMellowRen
Logu
 
It generally helps in questions like this, if you can tell us what parts of your code are not being tested.
 
[You can do this in the Developer Console. Run the test, see the results. Find your class (AccountListViewTaskCon) and its percentage in the table on the bottom right and double click. It will open the class and highlight all tested lines blue, untested lines red.]
 
That being said, I think one problem above is that you are starting the test ( system.Test.startTest(); ) after you instantiate the class ( objAccountListViewTaskConPageCon =  new AccountListViewTaskCon(); ) which means that the Constructor and all its code are probably not being covered in the test. Try swapping the order of those two lines.
 
Your next problem is that you have some try/catch structures in your code. This is good but in order to get 100% coverage you need to create a test that will deliberately create an error. This often means that the test will need a try/catch structure in order to catch the error you created without failing the test. You can google for examples. Sometimes this is really needed, sometimes it is more trouble than it is worth. If you have, for example, 98% coverage with the only red lines being the catch instructions you may decide to be happy with the test.
 
Regards
MellowRen
 
LoguLogu
Thanks for your reply . In VF page I am assigning values to those variables