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

Test Class for Twilio SMS Help Needed!
Hi all,
I have working Code the sends an SMS via Apex and Twilio, Great stuff. The trouble I am having is writing the Tests and I just need a little help. I don't do near enough Apex for my liking and I am feeling it here.
IAny help/pointers would be most appreciated.
Apex class:
public with sharing class Twilio {
Public string text1 {get;set;}
public boolean showmessage{get;set;}
public string selectedvalue {get;set;}
private final contact acct;
public Twilio(){}
public Twilio(ApexPages.StandardController stdController) {
this.acct = (contact)stdController.getRecord();
}
Public static Void sendfromtwilio (string Fromnumber,string text,string tonumber) {
errorResponseWrapper erw;
configuration_setting__c cstt=new configuration_setting__c();
cstt = [select Name,AccountSid__c, Active__c,AuthToken__c,Bulk_SMS__c,Contact_Phone_Number__c,Lead_Phone_Number__c,TestPhone__c FROM configuration_setting__c where Name='Twilio' limit 1];
String account = cstt.AccountSid__c;
String token = cstt.AuthToken__c;
HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.twilio.com/2010-04-01/Accounts/'+account+'/SMS/Messages.json');
req.setMethod('POST');
String VERSION = '3.2.0';
req.setHeader('X-Twilio-Client', 'salesforce-' + VERSION);
req.setHeader('User-Agent', 'twilio-salesforce/' + VERSION);
req.setHeader('Accept', 'application/json');
req.setHeader('Accept-Charset', 'utf-8');
req.setHeader('Authorization','Basic '+EncodingUtil.base64Encode(Blob.valueOf(account+':' +token)));
req.setBody('To='+EncodingUtil.urlEncode(+tonumber,'UTF-8')+'&From='+EncodingUtil.urlEncode(+Fromnumber,'UTF-8')+'&Body='+text);
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());
system.debug(res.getStatusCode());
if(res.getStatusCode()==201)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Confirm,'SMS Sent Successfully'));
} else{
erw =(errorResponseWrapper)json.deserialize(res.getBody(),errorResponseWrapper.class);
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,erw.message));
System.debug('ERROR');
}
}
public PageReference ok(){
PageReference acctPage = new PageReference('/' +acct.Id);
acctPage.setRedirect(true);
return acctPage;
}
public class errorResponseWrapper{
String code;
String message;
String moreInfo;
String status;
}
}
I have working Code the sends an SMS via Apex and Twilio, Great stuff. The trouble I am having is writing the Tests and I just need a little help. I don't do near enough Apex for my liking and I am feeling it here.
IAny help/pointers would be most appreciated.
Apex class:
public with sharing class Twilio {
Public string text1 {get;set;}
public boolean showmessage{get;set;}
public string selectedvalue {get;set;}
private final contact acct;
public Twilio(){}
public Twilio(ApexPages.StandardController stdController) {
this.acct = (contact)stdController.getRecord();
}
Public static Void sendfromtwilio (string Fromnumber,string text,string tonumber) {
errorResponseWrapper erw;
configuration_setting__c cstt=new configuration_setting__c();
cstt = [select Name,AccountSid__c, Active__c,AuthToken__c,Bulk_SMS__c,Contact_Phone_Number__c,Lead_Phone_Number__c,TestPhone__c FROM configuration_setting__c where Name='Twilio' limit 1];
String account = cstt.AccountSid__c;
String token = cstt.AuthToken__c;
HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.twilio.com/2010-04-01/Accounts/'+account+'/SMS/Messages.json');
req.setMethod('POST');
String VERSION = '3.2.0';
req.setHeader('X-Twilio-Client', 'salesforce-' + VERSION);
req.setHeader('User-Agent', 'twilio-salesforce/' + VERSION);
req.setHeader('Accept', 'application/json');
req.setHeader('Accept-Charset', 'utf-8');
req.setHeader('Authorization','Basic '+EncodingUtil.base64Encode(Blob.valueOf(account+':' +token)));
req.setBody('To='+EncodingUtil.urlEncode(+tonumber,'UTF-8')+'&From='+EncodingUtil.urlEncode(+Fromnumber,'UTF-8')+'&Body='+text);
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());
system.debug(res.getStatusCode());
if(res.getStatusCode()==201)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Confirm,'SMS Sent Successfully'));
} else{
erw =(errorResponseWrapper)json.deserialize(res.getBody(),errorResponseWrapper.class);
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,erw.message));
System.debug('ERROR');
}
}
public PageReference ok(){
PageReference acctPage = new PageReference('/' +acct.Id);
acctPage.setRedirect(true);
return acctPage;
}
public class errorResponseWrapper{
String code;
String message;
String moreInfo;
String status;
}
}
Create the below HttpCalloutMock class first:
Apex Class
@isTest
global class Twilio_MockClass implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest req) {
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setBody('{"example":"test"}');
res.setStatusCode(201);
return res;
}
}
Test Class:
@isTest
public class Twilio_Test {
Public static testmethod void testTwilio(){
Account acc = new Account(name = 'ABC');
/// Add the remaining Mandatory fields which are required to create the Account Record
insert acc;
Contact con = new contact(LastName = 'ABC Contact',AccountId = acc.Id);
/// Add the remaining Mandatory fields which are required to create the Contact Record
insert con;
configuration_setting__c cst = new configuration_setting__c();
cst.Name = 'Twilio';
cst.AccountSid__c = 'TestaccId'; /// Add the proper date here
cst.AuthToken__c = 'TestAuth'; /// Add the proper date here
insert cst;
Test.setMock(HttpCalloutMock.class, new Twilio_MockClass());
Twilio obj = new Twilio( new ApexPages.StandardController( con ) );
obj.text1 = 'Test';
obj.showmessage = true;
obj.selectedvalue = 'Test';
obj.ok();
Test.StartTest();
Twilio.sendfromtwilio('1234567890','Test message','0987654321');
Test.StopTest();
}
}
Thanks,
Maharajan.C
All Answers
Create the below HttpCalloutMock class first:
Apex Class
@isTest
global class Twilio_MockClass implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest req) {
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setBody('{"example":"test"}');
res.setStatusCode(201);
return res;
}
}
Test Class:
@isTest
public class Twilio_Test {
Public static testmethod void testTwilio(){
Account acc = new Account(name = 'ABC');
/// Add the remaining Mandatory fields which are required to create the Account Record
insert acc;
Contact con = new contact(LastName = 'ABC Contact',AccountId = acc.Id);
/// Add the remaining Mandatory fields which are required to create the Contact Record
insert con;
configuration_setting__c cst = new configuration_setting__c();
cst.Name = 'Twilio';
cst.AccountSid__c = 'TestaccId'; /// Add the proper date here
cst.AuthToken__c = 'TestAuth'; /// Add the proper date here
insert cst;
Test.setMock(HttpCalloutMock.class, new Twilio_MockClass());
Twilio obj = new Twilio( new ApexPages.StandardController( con ) );
obj.text1 = 'Test';
obj.showmessage = true;
obj.selectedvalue = 'Test';
obj.ok();
Test.StartTest();
Twilio.sendfromtwilio('1234567890','Test message','0987654321');
Test.StopTest();
}
}
Thanks,
Maharajan.C
Thanks For your help I am getting 90% test class run but my target is 95% can you make some chenges for me to get 95%...........