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
Tamojita GuhasarkarTamojita Guhasarkar 

Need some clarification with Test class

Hello Guys, 

I'm into a situation with a Test class , please help me to resolve it .

My Apex Code : -
public class Acc_dtls_nw 
{
    public String s;
    public integer i =0;
       
    public string acc_type_check(String Type_value)
    {
       string qry ='select count() from account where type=:Type_value';       
        i= database.countQuery(qry);
        if (i>0)
        {
           s='Total no of account Types are: '+i; 
        }
        else
        {
            s='Invalid type :';
        } 
              
          return s;     
         
    }}

I'm not able to get a 100% code coverage for this class. below part is not included under code coverage
 
 s='Total no of account Types are: '+i; 

Now the test class I wrote is below 
@istest
public class test_acc_dtls_nw 
{
     static testMethod void test_trucond()
    {
        Acc_dtls_nw ac= new Acc_dtls_nw();
        string tst_true = ac.acc_type_check('Prospect');
       // System.AssertEquals(ac.i,2);
        string p ='prospect';
       // System.AssertEquals(ac.s,p);      
    }
    
    static testMethod void test_flscond()
    {
        Acc_dtls_nw ac= new Acc_dtls_nw();
        string tst_flse = ac.acc_type_check('xyz');
       // System.AssertEquals(ac.i,0);        
    }
}

So why that part is getting missed under code coverage? is it because S is not doing anything and used just for strong some text value? please help me to get this clarified. 
Best Answer chosen by Tamojita Guhasarkar
Amit Singh 1Amit Singh 1
You need to insert an Account beacue test class do not have access to the database by default.

Use below test class.
@istest
public class test_acc_dtls_nw 
{
     static testMethod void test_trucond()
    {
       Account acc = new Account(Name='Test Account', Type='Prospect');
        insert acc; // If some fields are rewuired then insert value in those fields as well.
        Acc_dtls_nw ac= new Acc_dtls_nw();
        string tst_true = ac.acc_type_check('Prospect');
       // System.AssertEquals(ac.i,2);
        string p ='prospect';
       // System.AssertEquals(ac.s,p);      
    }
    
    static testMethod void test_flscond()
    {
        Acc_dtls_nw ac= new Acc_dtls_nw();
        string tst_flse = ac.acc_type_check('xyz');
       // System.AssertEquals(ac.i,0);        
    }
}
Also, visit below links.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm

Please let us know if thi helps :)
Thanks,
AMit Singh

All Answers

DeveloperSudDeveloperSud
Hi ,

Is that Acc_dtls_nw  class you created working properly? I checked in the method acc_type_check. Do you able to use that parameter  Type_value? I think you need to use something like this : string qry ='select count() from account where type=:'+Type_value;  
Amit Singh 1Amit Singh 1
You need to insert an Account beacue test class do not have access to the database by default.

Use below test class.
@istest
public class test_acc_dtls_nw 
{
     static testMethod void test_trucond()
    {
       Account acc = new Account(Name='Test Account', Type='Prospect');
        insert acc; // If some fields are rewuired then insert value in those fields as well.
        Acc_dtls_nw ac= new Acc_dtls_nw();
        string tst_true = ac.acc_type_check('Prospect');
       // System.AssertEquals(ac.i,2);
        string p ='prospect';
       // System.AssertEquals(ac.s,p);      
    }
    
    static testMethod void test_flscond()
    {
        Acc_dtls_nw ac= new Acc_dtls_nw();
        string tst_flse = ac.acc_type_check('xyz');
       // System.AssertEquals(ac.i,0);        
    }
}
Also, visit below links.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm

Please let us know if thi helps :)
Thanks,
AMit Singh
This was selected as the best answer
Tamojita GuhasarkarTamojita Guhasarkar
Hi Amit ,

Ok so it mandatory to insert record(s) in test class if Im using object records in apex class? 

Thanks,