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
Aditi Mohanty 5Aditi Mohanty 5 

i need test class for this. I am not getting above 75% coverage

global class Batch_AddConToAcc implements Database.Batchable <sObject> {
     
    List<contact> lstCon = new List<Contact>();
   String query = 'SELECT Id, Name FROM Account WHERE checkacc__c = True';
    global Database.QueryLocator start(Database.BatchableContext bc) {
         
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext bc,List<Account> batch) {
       List<Account> acnt=new List<Account>();
        for (Account a : batch) {
            if(a.checkacc__c = True){
            Contact c =  new Contact();
            c.LastName = a.Name;
            c.AccountId = a.Id;
            lstCon.add(c);
        }
        INSERT lstCon;
        }
        for (Account a : batch){
          a.checkacc__c = False;  
            acnt.add(a);
        } 
        update acnt;
        
    }
    
    global void finish(Database.BatchableContext bc) {
     

AsyncApexJob ajob = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,
                          TotalJobItems, CreatedBy.Email
                          FROM AsyncApexJob WHERE Id =
                          :bc.getJobId()];
        if(ajob.Status=='Completed'){
    
   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
   String[] toAddresses = new String[] {ajob.CreatedBy.Email};
   mail.setToAddresses(toAddresses);
   mail.setSubject('Apex Sharing Recalculation ' + ajob.Status);
   mail.setPlainTextBody
   ('The batch Apex job processed ' + ajob.TotalJobItems +
   ' batches status '+ajob.Status );
   Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            }
            
            else{
                
    
   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
   String[] toAddresses = new String[] {ajob.CreatedBy.Email};
   mail.setToAddresses(toAddresses);
   mail.setSubject('Apex Sharing Recalculation ' + ajob.Status);
   mail.setPlainTextBody
   ('The batch Apex job processed ' + ajob.TotalJobItems +
   ' batches with '+ ajob.NumberOfErrors + ' failures.');
   Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                
            }
        }     
       
    }

 
SwethaSwetha (Salesforce Developers) 
HI Aditi,
Can you post the test class code you have written so far and highlight the lines of code (from class in bold)that need coverage so the community can help.

If you are looking for writing test class from scratch, the below post should help you get started
Code Coverage Best Practices
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_code_coverage_best_pract.htm

Checking Code Coverage
https://help.salesforce.com/articleView?id=code_dev_console_tests_coverage.htm&type=5

Deployment related code coverage issues
https://help.salesforce.com/articleView?id=000335222&type=1&mode=1

https://salesforce.stackexchange.com/questions/148894/help-required-to-get-full-code-covered-in-test-class

https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines 

If this information helps, please mark this answer as best.Thanks
Aditi Mohanty 5Aditi Mohanty 5
@isTest
public class Test_Batch_AddConToAcc {
    
    static testMethod void testMethod1() {
        List<Account> lstAccount= new List<Account>();
        for(Integer i=0 ;i <5;i++) {
            Account acc = new Account();
            acc.Name ='Name'+i;
            lstAccount.add(acc);
        }
        
        INSERT lstAccount;
        
        Test.startTest();
        
        Batch_AddConToAcc obj = new Batch_AddConToAcc();
        DataBase.executeBatch(obj); 
        
        Test.stopTest();
    }
}

 
Maharajan CMaharajan C
@isTest
public class Test_Batch_AddConToAcc {
    
    static testMethod void testMethod1() {
        List<Account> lstAccount= new List<Account>();
        for(Integer i=0 ;i <5;i++) {
            Account acc = new Account();
            acc.Name ='Name'+i;
            acc.checkacc__c = True;
            lstAccount.add(acc);
        }
        
        INSERT lstAccount;
        
        Test.startTest();
        
        Batch_AddConToAcc obj = new Batch_AddConToAcc();
        DataBase.executeBatch(obj); 
        
        Test.stopTest();
    }
}

add  acc.checkacc__c = True while inserting the Account in test class.

Thanks,
Maharajan.C