You need to sign in to do that
Don't have an account?

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
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
Please try below batch job Let us know if this will help you
All Answers
Please try below batch job Let us know if this will help you