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

Test class for batch apex for closed opportunity
Can anyone provide Test class for following use Case.
Consider all positive negative and bulk test case
Use case ::
Collection of Closed Won Opportunities
1. The Primary contact associated with an account needs to know which all are the closed won opportunities for his account per day.
2. Create a process which will run every day in the midnight & collect all the Closed won opportunities of that account in a day & send an email to Primary Contact.
3. The email body should contain the Opportunity Name & it's respective amount with the aggregated amount in the end.
-----------------------------------------------------------------------Batch Class-------------------------------------------------------------
global class ContactWithClosedWonOpportunities
implements Database.Batchable<sObject>, Database.Stateful {
/**
This Map will store the contact email IDs and respective List of Opportunites.
*/
Map<String,List<Opportunity>> contactOpprtunitiesMap =
new Map<String,List<Opportunity>>();
/**
This method will Query all Contacts associate with Account having Closed Won Opportunities.
@return Database.QueryLocator
*/
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator(
'SELECT ' +
'Name, ' +
'( SELECT Email FROM Contacts WHERE Preferred_Contact__c = TRUE LIMIT 1 ), ' +
'( SELECT Name, Amount FROM Opportunities WHERE StageName LIKE \'Closed Won\') ' +
'FROM ' +
'Account ' +
'WHERE ' +
'id ' +
'IN ' +
'(SELECT AccountId FROM Contact WHERE Preferred_Contact__c = TRUE)'
);
}
/**
This method will add the values in contactOpportunitesMap.
@return Nothing.
*/
global void execute(Database.BatchableContext BC, List<sObject> scope) {
List<Account> accountList = (List<Account>) scope;
//System.debug(scope + '------------------------- ');
for(Account accountRecord : accountList) {
if(accountRecord.Contacts[0].email != null) {
contactOpprtunitiesMap.put(
accountRecord.Contacts[0].email,
accountRecord.Opportunities
);
}
}
}
/**
This method will send to each primary contact with Opportunity name and Amount.
@return Nothing.
*/
global void finish(Database.BatchableContext BC) {
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
for( String mailId : contactOpprtunitiesMap.keySet() ) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
List<String> sendTo = new List<String>();
sendTo.add(mailId);
mail.setToAddresses(sendTo);
mail.setReplyTo('ayisa@gmail.com');
mail.setSubject('All Closed Won Opportunities');
String body = '<html><body>Dear Sir/Mam, <br/><br/>';
body += 'All Closed Won Opportunities are - <br/>';
Decimal total = 0;
for( Opportunity opportunityRecord : contactOpprtunitiesMap.get(mailId) ) {
body += ' ' + opportunityRecord.Name + ': ' +
opportunityRecord.Amount + '<br/>';
total += opportunityRecord.Amount;
}
body += '<br/> <b>Total: ' + total + '</b></body></html>';
mail.setHtmlBody(body);
System.debug(body);
mails.add(mail);
}
Messaging.sendEmail(mails);
}
}
Consider all positive negative and bulk test case
Use case ::
Collection of Closed Won Opportunities
1. The Primary contact associated with an account needs to know which all are the closed won opportunities for his account per day.
2. Create a process which will run every day in the midnight & collect all the Closed won opportunities of that account in a day & send an email to Primary Contact.
3. The email body should contain the Opportunity Name & it's respective amount with the aggregated amount in the end.
-----------------------------------------------------------------------Batch Class-------------------------------------------------------------
global class ContactWithClosedWonOpportunities
implements Database.Batchable<sObject>, Database.Stateful {
/**
This Map will store the contact email IDs and respective List of Opportunites.
*/
Map<String,List<Opportunity>> contactOpprtunitiesMap =
new Map<String,List<Opportunity>>();
/**
This method will Query all Contacts associate with Account having Closed Won Opportunities.
@return Database.QueryLocator
*/
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator(
'SELECT ' +
'Name, ' +
'( SELECT Email FROM Contacts WHERE Preferred_Contact__c = TRUE LIMIT 1 ), ' +
'( SELECT Name, Amount FROM Opportunities WHERE StageName LIKE \'Closed Won\') ' +
'FROM ' +
'Account ' +
'WHERE ' +
'id ' +
'IN ' +
'(SELECT AccountId FROM Contact WHERE Preferred_Contact__c = TRUE)'
);
}
/**
This method will add the values in contactOpportunitesMap.
@return Nothing.
*/
global void execute(Database.BatchableContext BC, List<sObject> scope) {
List<Account> accountList = (List<Account>) scope;
//System.debug(scope + '------------------------- ');
for(Account accountRecord : accountList) {
if(accountRecord.Contacts[0].email != null) {
contactOpprtunitiesMap.put(
accountRecord.Contacts[0].email,
accountRecord.Opportunities
);
}
}
}
/**
This method will send to each primary contact with Opportunity name and Amount.
@return Nothing.
*/
global void finish(Database.BatchableContext BC) {
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
for( String mailId : contactOpprtunitiesMap.keySet() ) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
List<String> sendTo = new List<String>();
sendTo.add(mailId);
mail.setToAddresses(sendTo);
mail.setReplyTo('ayisa@gmail.com');
mail.setSubject('All Closed Won Opportunities');
String body = '<html><body>Dear Sir/Mam, <br/><br/>';
body += 'All Closed Won Opportunities are - <br/>';
Decimal total = 0;
for( Opportunity opportunityRecord : contactOpprtunitiesMap.get(mailId) ) {
body += ' ' + opportunityRecord.Name + ': ' +
opportunityRecord.Amount + '<br/>';
total += opportunityRecord.Amount;
}
body += '<br/> <b>Total: ' + total + '</b></body></html>';
mail.setHtmlBody(body);
System.debug(body);
mails.add(mail);
}
Messaging.sendEmail(mails);
}
}
use this code

The code coverage is only 40%. Can you cover Finish method as well