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
sai kumar 433sai kumar 433 

plz execute this test class code coverage should above 75 thanks advance......


plz execute this test class i want code coverage above 75 thanks advance
class

global class  BatchClass5 implements  Database.Batchable<sObject> {
   global final string query;
  global  BatchClass5(String q)
      {
         query = q;
      }
      
      global Database.QueryLocator start(Database.BatchableContext BC){
      
         return Database.getQueryLocator(query);
      }
      
      global void execute(Database.BatchableContext BC, List<Account> scope)
      {
          List<Account> accns = new List<Account>();
       List<Contact> lstcon= new List<contact>();
         for(Account a : scope)
         {
                 Account a1 = new Account();
                 contact c = new contact();
                 c.lastname = a1.name;
                 c.accountid = a1.id; 
                  lstcon.add(c);
                  }
          
      insert lstcon;
      }
      global void finish(Database.BatchableContext BC){
        // Get the ID of the AsyncApexJob representing this batch job  
        // from Database.BatchableContext.    
        // Query the AsyncApexJob object to retrieve the current job's information.  
       
      }
}

Test class

@isTest
private class BatchClass5_Test5 
{
    static testmethod void m1()
    {
        
        Test.startTest();
        Account a = new Account(Name='saikumar');
            insert a;
        contact c = new Contact();
        c.LastName =a.Name;
        c.AccountId= a.Id;
        insert c;
                system.assertEquals('saikumar',c.LastName);

        BatchClass5 sync = new batchclass5('select id,name from Account');
        ID batchprocessid = Database.executeBatch(sync);
        Test.stopTest();
    }

}
Amit Chaudhary 8Amit Chaudhary 8
Please update your batch job like below if you just want to create contact only
global class  BatchClass5 implements  Database.Batchable<sObject> 
{
	global final string query;
	global  BatchClass5(String q)
    {
         query = q ;
    }
      
	global Database.QueryLocator start(Database.BatchableContext BC)
	{
		return Database.getQueryLocator(query);
	}

	global void execute(Database.BatchableContext BC, List<Account> scope)
	{
	
		List<Contact> lstcon= new List<contact>();
		for(Account a : scope)
		{
			Contact c = new contact();
			c.lastname = a.name;
			c.accountid = a.id; 
			lstcon.add(c);
		}
		insert lstcon;
	}
	  
	global void finish(Database.BatchableContext BC){
	}
}
TEst class like below
@isTest
private class BatchClass5_Test5 
{
    static testmethod void m1()
    {
        Account a = new Account(Name='saikumar');
        insert a;
        
        Test.startTest();

			BatchClass5 sync = new batchclass5('select id,name from Account');
			ID batchprocessid = Database.executeBatch(sync);
			
        Test.stopTest();
    }

}


Let us know if this will help you