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
srikanth cheera 6srikanth cheera 6 

please check the code and write test class

global class Task020 implements database.Batchable<sobject>{
 
    global database.QueryLocator start(database.BatchableContext bc){
        return database.getQueryLocator('select id,name from Account where createddate= Today()');
    }
    global void execute(database.BatchableContext bc,list<Account> acc){
        for(Account a:acc){
            }
        delete acc;
        }
        
        global void finish(database.BatchableContext bc){
            
        }
    }
v varaprasadv varaprasad
Hi Srikanth,

Please try once below code : 
 
@isTest 
public class Task020Test 
{
    static testMethod void testMethod1() 
    {
        List<Account> lstAccount= new List<Account>();
        for(Integer i=0 ;i <200;i++)
        {
            Account acc = new Account();
            acc.Name ='Name'+i;
            lstAccount.add(acc);
        }
        
        insert lstAccount;
        
        Test.startTest();

            Task020 obj = new Task020();
            DataBase.executeBatch(obj); 
            
        Test.stopTest();
    }
}

Thanks
Varaprasad
srikanth cheera 6srikanth cheera 6
it's showing 33% bro try once 
v varaprasadv varaprasad
Hi Srikanth,

Please check once below code :




 
=====Batch Apex ======

global class Task020 implements database.Batchable<sobject>{
 
    global database.QueryLocator start(database.BatchableContext bc){
        return database.getQueryLocator('select id,name from Account where createddate = Today');
    }
    global void execute(database.BatchableContext bc,list<Account> acc){
        system.debug('==acc=='+acc.size());
       
        delete acc;
        }
        
        global void finish(database.BatchableContext bc){
            
        }
    }

=====Batch Apex its test class======

@isTest 
public class Task020Test 
{
    static testMethod void testMethod1() 
    {
        List<Account> lstAccount= new List<Account>();
        for(Integer i=0 ;i <200;i++)
        {
            Account acc = new Account();
            acc.Name ='Name'+i;
            lstAccount.add(acc);
        }
        
        insert lstAccount;
        
        Test.startTest();

            Task020 obj = new Task020();
            DataBase.executeBatch(obj); 
            
        Test.stopTest();
    }
}

Hope this helps you.

Thanks
varaprasad

 
srikanth cheera 6srikanth cheera 6
No bro getting 40% code coverage....tq for u r patience
v varaprasadv varaprasad
Hello srikanth,

Please change your batch apex class like above your class is having an issue with the query like today.
=====Batch Apex ======

global class Task020 implements database.Batchable<sobject>{
 
    global database.QueryLocator start(database.BatchableContext bc){
        return database.getQueryLocator('select id,name from Account where createddate = Today');
    }
    global void execute(database.BatchableContext bc,list<Account> acc){
        system.debug('==acc=='+acc.size());
       
        delete acc;
        }
        
        global void finish(database.BatchableContext bc){
            
        }
    }

=====Batch Apex its test class======

@isTest 
public class Task020Test 
{
    static testMethod void testMethod1() 
    {
        List<Account> lstAccount= new List<Account>();
        for(Integer i=0 ;i <200;i++)
        {
            Account acc = new Account();
            acc.Name ='Name'+i;
            lstAccount.add(acc);
        }
        
        insert lstAccount;
        
        Test.startTest();

            Task020 obj = new Task020();
            DataBase.executeBatch(obj); 
            
        Test.stopTest();
    }
}

Please add above batch class first and then run your test class.


Thanks
Varaprasad 










 
Amit Chaudhary 8Amit Chaudhary 8
Hi ,

Please do the below changes in your Batch job first
1) Change the query from  select id,name from Account where createddate= Today()
to
select id,name from Account where createddate = Today
Please check below post for date filter in query
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm

2) No need to below code as you are delete all record in your batch job
        for(Account a:acc){
         }

You Batch job should be like below
global class Task020 implements database.Batchable<sobject>{
 
    global database.QueryLocator start(database.BatchableContext bc){
        return database.getQueryLocator('select id,name from Account where createddate = Today');
    }
    global void execute(database.BatchableContext bc,list<Account> acc){
      delete acc;
    }
    global void finish(database.BatchableContext bc){
    }
}

I will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing

Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm
3) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

Test Class for Batch job
1) http://amitsalesforce.blogspot.com/search/label/Batch%20Job

Sample test class to start
@isTest 
public class Task020_Test 
{
    static testMethod void myUnitTest() 
    {
        Account acc = new Account();
        acc.Name ='Test Account';
        insert acc;
        
        Test.startTest();

            Task020 obj = new Task020();
            DataBase.executeBatch(obj); 
            
        Test.stopTest();
        
        List<Account> lstAcc = [Select Id from Account ];
        System.assert(lstAcc.size() ==0);
        
    }
}
NOTE:- Always add Assert in Test class to validate the result
i tested the same code and working fine in my dev org

Let me know if this will help you