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
nallurisivashankarnallurisivashankar 

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)
    {
    }
}

SRKSRK
try this link
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
nallurisivashankarnallurisivashankar
HI,

Thanks for your reply..

still i am facing the problem.

can you please post the code.

--
THANKS&REGARDS
Shiva Shankar Nalluri
SRKSRK
try this you might get some error if there are more the 200 records income a result of that query in start


@istest(seealldata=true)
class Test_ClassName
{
static testmethod void test()
{
Test.startTest();
Database.executebatch(new expireNotify());
Test.stopTest();

}
nallurisivashankarnallurisivashankar
Hi,
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&REGARDS
Shiva Shankar Nalluri