You need to sign in to do that
Don't have an account?
Rajesh SFDC
how to write test class for web services
global class CampaignReminder
{
WebService static void SendReminder(string id)
{
id cmpid = id;
Integer batchSize = 1;
database.executebatch(new SendeMailinBatch(cmpid) , batchSize);
}
}
========================================================
batch apex process
===================
global class SendeMailinBatch implements Database.Batchable<sObject>,Database.Stateful
{
Public id campaignid;
global SendeMailinBatch(id campid)
{
campaignid = campid;
}
global Database.QueryLocator start(Database.BatchableContext BC)
{
//Batch on all campaign memeber who belongs to the requetsed campaign
String query = 'select id, emailtobesentid__c from campaignmember where campaignid = :campaignid';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope)
{
//*** Select the campaign details
List<campaignmember> cmx = new list<campaignmember>();
campaign cmp = new campaign();
cmp = [ select id, From_eMail__c, Reply_To__c, templatetobesent__c from campaign where id = :campaignid ];
for(Sobject s : scope)
{
//Type cast sObject in campaign object
campaignmember CM = (campaignmember)s ;
//Sending Mail
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setSenderDisplayName(cmp.From_eMail__c);
mail.setReplyTo(cmp.Reply_To__c);
mail.setTargetObjectId(CM.emailtobesentid__c);
mail.setTemplateId(cmp.templatetobesent__c);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
}
}
global void finish(Database.BatchableContext BC)
{
}
}
======================================
TEST CLASS
@isTest(seealldata=true)
private class testCampaignReminder
{
static testMethod void testMyWebSvc()
{
CampaignReminder cm = new CampaignReminder();
// cm.id='123';
//cm.Name='test1';
// SendReminder sr=new SendReminder();
// sr.Name='test1';
//CampaignReminder.SendReminder( cm );
Campaign[] a = [select id from Campaign where Name= 'confirmation2days'];
System.assertEquals(a.size(), 1);
//database.executebatch(new SendeMailinBatch(cmpid) , batchSize);
}
}
{
WebService static void SendReminder(string id)
{
id cmpid = id;
Integer batchSize = 1;
database.executebatch(new SendeMailinBatch(cmpid) , batchSize);
}
}
========================================================
batch apex process
===================
global class SendeMailinBatch implements Database.Batchable<sObject>,Database.Stateful
{
Public id campaignid;
global SendeMailinBatch(id campid)
{
campaignid = campid;
}
global Database.QueryLocator start(Database.BatchableContext BC)
{
//Batch on all campaign memeber who belongs to the requetsed campaign
String query = 'select id, emailtobesentid__c from campaignmember where campaignid = :campaignid';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope)
{
//*** Select the campaign details
List<campaignmember> cmx = new list<campaignmember>();
campaign cmp = new campaign();
cmp = [ select id, From_eMail__c, Reply_To__c, templatetobesent__c from campaign where id = :campaignid ];
for(Sobject s : scope)
{
//Type cast sObject in campaign object
campaignmember CM = (campaignmember)s ;
//Sending Mail
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setSenderDisplayName(cmp.From_eMail__c);
mail.setReplyTo(cmp.Reply_To__c);
mail.setTargetObjectId(CM.emailtobesentid__c);
mail.setTemplateId(cmp.templatetobesent__c);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
}
}
global void finish(Database.BatchableContext BC)
{
}
}
======================================
TEST CLASS
@isTest(seealldata=true)
private class testCampaignReminder
{
static testMethod void testMyWebSvc()
{
CampaignReminder cm = new CampaignReminder();
// cm.id='123';
//cm.Name='test1';
// SendReminder sr=new SendReminder();
// sr.Name='test1';
//CampaignReminder.SendReminder( cm );
Campaign[] a = [select id from Campaign where Name= 'confirmation2days'];
System.assertEquals(a.size(), 1);
//database.executebatch(new SendeMailinBatch(cmpid) , batchSize);
}
}
Please find the link http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_wsdl2apex_testing.htm where you will find some pretty good examle suggested buy salesforce!