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
Anna Antonella Adiletta 1Anna Antonella Adiletta 1 

How to write a test class for this batch apex

 I'm trying to write a test class for this batch apex. help me please

global class BatchAggiornaStatoAccount implements Database.Batchable<sObject>, Database.AllowsCallouts{    
    public Date d = System.today()-365;
 
    global Database.QueryLocator start(Database.BatchableContext BC){
       // Date d = System.today()-365;
        String query = 'SELECT Id, AccountId, Ordine__c, Data_Ordine__c From Quote WHERE account.stato__c = \'Cliente\' and data_ordine__c <:d';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Quote> scope){
        set<id> IdAccount = new set<id>();
        list<Account> ListToUpdate = new list<Account>();
        map<Account,list<quote>> QuotesPerAccountId = new map<Account,list<quote>>();
        For(Quote q:scope)
            IdAccount.add(q.AccountId);
        
        List<quote> lquote = [Select id,Accountid,Account.stato__c,Data_Ordine__c from quote where Accountid in:IdAccount];
        For(quote q:lquote){
            If(QuotesPerAccountId.get(q.Account) == null)
                QuotesPerAccountId.put(q.Account,new list<quote>());
                
            QuotesPerAccountId.get(q.Account).add(q);
        }
        
        For(Account acc:QuotesPerAccountId.keyset()){
            Boolean cliente = true;
            For(Quote q:QuotesPerAccountId.get(acc)){
                If(q.Data_Ordine__c > d){
                    cliente = false;
                    break;
                }
            }
            if(cliente){
                acc.stato__c = 'Dormiente';
                ListToUpdate.add(acc);
            }
        }
        
        update ListToUpdate;
    }
    
    global void finish(Database.BatchableContext BC){
     
    }
}



@isTest
private class BatchAggiornaStato_Test {
   static testMethod void testBatchClass(){
       BatchAggiornaStatoAccount bc = new BatchAggiornaStatoAccount();
       
       
    set<id> IdAccount = new set<id>();  
    map<Account,list<quote>> QuotesPerAccountId = new map<Account,list<quote>>();
      
       Account acc = new Account(LastName='Magneti',FirstName='Pippo', Cliente__c=false, Stato__c='Dormiente', Data_primo_ordine__c=date.Valueof('2016-08-29') );
       insert acc;
       List<Account> lAccount = [SELECT Name, Stato__c, Cliente__c FROM Account WHERE id=:IdAccount];
      
       List<Opportunity> Opportunities = new List <Opportunity>();
       Opportunity opp = new Opportunity(Name='Corso',StageName='chiusa',CloseDate=date.valueof('2016-09-02'));
       insert opp;
       opportunities.add(opp);
     
       Quote qt = new Quote (Name='pippo', OpportunityId=opp.id,Ordine__c= false, Data_Ordine__c=date.Valueof('2016-09-06'));
       insert qt;
       List<quote> lquote = [Select id,Accountid,Account.stato__c, Account.Cliente__c,Data_Ordine__c from quote where AccountId in:IdAccount];
       
 
      
    BatchAggiornaStatoAccount BatchAggiornaStato_Test = new BatchAggiornaStatoAccount();
Database.executeBatch(BatchAggiornaStato_Test);

}
}
       
The code coverage is 19%.
How i can to continue in write this test class.
Thank you

 
Best Answer chosen by Anna Antonella Adiletta 1
Amit Chaudhary 8Amit Chaudhary 8
You need to setup data according to your Start method
Please try below batch job
@isTest
private class BatchAggiornaStato_Test 
{
   static testMethod void testBatchClass()
   {
		Account acc = new Account(LastName='Magneti',FirstName='Pippo', Cliente__c=false, Stato__c='Cliente', Data_primo_ordine__c=date.Valueof('2016-08-29') );
		insert acc;

		Opportunity opp = new Opportunity(Accountid = acc.id,Name='Corso',StageName='chiusa',CloseDate=date.valueof('2016-09-02'));
		insert opp;

		Quote qt = new Quote (Name='pippo', OpportunityId=opp.id,Ordine__c= false, Data_Ordine__c=date.Valueof('2014-09-06'));
		insert qt;

		BatchAggiornaStatoAccount BatchAggiornaStato_Test = new BatchAggiornaStatoAccount();
		Database.executeBatch(BatchAggiornaStato_Test);

	}
}
Let us know if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
You need to setup data according to your Start method
Please try below batch job
@isTest
private class BatchAggiornaStato_Test 
{
   static testMethod void testBatchClass()
   {
		Account acc = new Account(LastName='Magneti',FirstName='Pippo', Cliente__c=false, Stato__c='Cliente', Data_primo_ordine__c=date.Valueof('2016-08-29') );
		insert acc;

		Opportunity opp = new Opportunity(Accountid = acc.id,Name='Corso',StageName='chiusa',CloseDate=date.valueof('2016-09-02'));
		insert opp;

		Quote qt = new Quote (Name='pippo', OpportunityId=opp.id,Ordine__c= false, Data_Ordine__c=date.Valueof('2014-09-06'));
		insert qt;

		BatchAggiornaStatoAccount BatchAggiornaStato_Test = new BatchAggiornaStatoAccount();
		Database.executeBatch(BatchAggiornaStato_Test);

	}
}
Let us know if this will help you
 
This was selected as the best answer
Anna Antonella Adiletta 1Anna Antonella Adiletta 1
It's wonderful. The code coverage is 92%. Thank you very much