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
ganesh jadhavganesh jadhav 

How to Increase code coverage

Hi I'm trying to write test cases using isTest annotation

first of all i have initialized my visualforce page.

than initialized the controller

and iam calling each methods in controller

 

but in my code coverage is only 85% only....... i know 75% code coverage is enough for deploy code in production organization but iam expecting 100% code coverage here is my code please let me any suggesions

 

Here is mine Controller

 

   public class MyQueryController{

   public Set<String>myQueryResults { get; Public set; }  

   public List<Fund_Management__c> getUniquedata(){

   myQueryResults= new Set<String>();  

    List<Fund_Management__c> query=[SELECT Fund_Name__c FROM Fund_Management__c];

    System.debug('Results'+query);  9    for(Fund_Management__c results : query){  

    myQueryResults.add(results.Fund_Name__c); 

   }  

    return query;

    }  

  }

 

highlighted area is not get covered anybody knows to get code coverage for that area..... and also let me know how to increase code coverage for code included in conditional statements like  for, if, else conditions

 

 

 

And here is mine test class

 

@isTest
Private class MyQueryControllerTest{
    Static testmethod void UnitTest(){
    Set<String> myQueryResults = new Set<String>{};
    List<Fund_Management__c> query=[SELECT Fund_Name__c FROM Fund_Management__c];
    //initialized the vf page here
    PageReference pageref = Page.MyQueryPage;
    Test.setCurrentPageReference(pageref);
    //initialized the controller
    MyQueryController myqc = new MyQueryController();
    //calling methods in controller
    myqc.getUniquedata();
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
hpereirahpereira

Hello,

 

When you write test classes, the framework assumes data your database is empty (except for some special standard objects).

So your query ([SELECT Fund_Name__c FROM Fund_Management__c]) is returning no records and that's why the line inside the for cycle is not being tested.

 

You can force the test class to access the real content of the database, but it is not the best way (not advisable).

 

What you should do is, in the test class, before calling method "getUniquedata", just create and insert in DB an object of type Fund_Management__c.

This is very simple and it's the right way to do it. :)

 

All Answers

hpereirahpereira

Hello,

 

When you write test classes, the framework assumes data your database is empty (except for some special standard objects).

So your query ([SELECT Fund_Name__c FROM Fund_Management__c]) is returning no records and that's why the line inside the for cycle is not being tested.

 

You can force the test class to access the real content of the database, but it is not the best way (not advisable).

 

What you should do is, in the test class, before calling method "getUniquedata", just create and insert in DB an object of type Fund_Management__c.

This is very simple and it's the right way to do it. :)

 

This was selected as the best answer
ganesh jadhavganesh jadhav

Thank you so much

i have added the code for insertion first and after that selection code

than mine code coverage is 100% :)

 

hpereirahpereira
Thank you for marking my answer as "solved".
Could you also give me kudos (click the star image) if you consider the response was indeed helpful?
Best regards