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
aressaress 

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 += '&nbsp;&nbsp;&nbsp;&nbsp;' + opportunityRecord.Name + ': ' +
opportunityRecord.Amount + '<br/>';
total += opportunityRecord.Amount;
}
body += '<br/>&nbsp;&nbsp;&nbsp;&nbsp; <b>Total: ' + total + '</b></body></html>';
mail.setHtmlBody(body);
System.debug(body);
mails.add(mail);
}
Messaging.sendEmail(mails);
}
}
Raj VakatiRaj Vakati
use this code 
@isTest
public class ContactWithClosedWonOpportunities_Test {



@isTest
private static void ContactWithClosedWonOpportunitiesBatch(){

	Test.startTest();
	BatchApexDemo bd = new BatchApexDemo();
	List<Account> acc = new List<Account>{
		new Account(
					Name= 'Test Account', 
					BillingStreet= 'Street No 12', 
					BillingCity= 'Pune', 
					BillingState= 'MH'
				)
			};
			insert acc;

	List<Contact> conn = new List<Contact>{
		new Contact(LastName= 'Test', 
				FirstName= 'Test1', 
				Preferred_Contact__c =true ,
				AccountId= acc[0].Id)
	};
	insert conn;
	
	Opportunity opp = new Opportunity(Name='test opp', StageName='stage', Probability = 95, CloseDate=system.today());

opp.Amount = 1000;
 opp.AccountId= acc[0].Id)
opp.StageName = 'Closed/Won';
insert opp;



   Database.executeBatch(new ContactWithClosedWonOpportunities());

	Test.stopTest();
}

}

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