You need to sign in to do that
Don't have an account?
test class for batch apex
Hi,
I have created batch class, but i am facing problem during code coverage.
can any one tell me how to write test class?
here is my class:
global class expireNotify implements Database.Batchable<sObject>
{
global Database.QueryLocator start(Database.BatchableContext bc)
{
Date d = Date.today();
String soql = 'SELECT Expiry_Date__c, Name, Email_Address__c FROM Member__c WHERE Expiry_Date__c =: d';
return Database.getQueryLocator(soql);
}
global void execute(Database.BatchableContext bc, List<Member__c> recs)
{
List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
for(Member__c m : recs)
{
List<String> toAddresses = new List<String>();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
toAddresses.add(m.Email_Address__c);
mail.setToAddresses(toAddresses);
mail.setSubject('Welcome to Sweet 16 Siebel Batch');
String messageBody = '<html><body>Hi ' + m.Name + ',<br>Your account Expires today. <br>Kindly contact your administrator.<br><br><b>Regards,</b><br>Magulan D</body></html>';
mail.setHtmlBody(messageBody);
mailList.add(mail);
}
Messaging.sendEmail(mailList);
}
global void finish(Database.BatchableContext bc)
{
}
}
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm
In the end they enplane a test method also
let me know if you still have the problem
Thanks for your reply..
still i am facing the problem.
can you please post the code.
--
THANKS®ARDS
Shiva Shankar Nalluri
@istest(seealldata=true)
class Test_ClassName
{
static testmethod void test()
{
Test.startTest();
Database.executebatch(new expireNotify());
Test.stopTest();
}
by doing this the code coverage is 29% only.pls find the code below.
//
global class expireNotify implements Database.Batchable
2 { 3 global Database.QueryLocator start(Database.BatchableContext bc)
4 { 5 Date d = Date.today(); 6 String soql = 'SELECT Expiry_Date__c,
Name, Email_Address__c FROM Member__c WHERE Expiry_Date__c =: d'; 7
return Database.getQueryLocator(soql); 8 } 9 10 global void
execute(Database.BatchableContext bc, List recs) 11 { 12
List mailList = new
List(); 13 for(Member__c m : recs) 14 { 15
List toAddresses = new List(); 16
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 17
toAddresses.add(m.Email_Address__c); 18 mail.setToAddresses(toAddresses);
19 mail.setSubject('Welcome to Sweet 16 Siebel Batch'); 20 String
messageBody = 'Hi ' + m.Name + ',
Your account Expires
today.
Kindly contact your
administrator.
Regards,
Magulan D'; 21
mail.setHtmlBody(messageBody); 22 mailList.add(mail); 23 } 24
Messaging.sendEmail(mailList); 25 } 26 27 global void
finish(Database.BatchableContext bc) 28 { 29 } 30 }
--
THANKS®ARDS
Shiva Shankar Nalluri