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

Unable to do Code Coverage
Hi,
Can anyone help me with code coverage.
Class:
public class SMSTask {
public string strMobNo{ get; set; }
public SMSTask(ApexPages.StandardController controller){
String MBRid;
}
public SMSTask () { }
Public void sendMessage(){
Http http = new Http();
HttpRequest req = new HttpRequest();
String fromNum='918973147939';
//String toNum= '919790882125';
strMobNo = '919790882125';
req.setEndpoint('http://www.myvaluefirst.com/smpp/sendsms?username=dempeerlesshttp&password=demo4321&to=91'+strMobNo+'&from='+fromNum+'&text=hi');
req.setMethod('GET');
if (test.isRunningTest()!=true) HttpResponse res = http.send(req);
//XMLDom responseXML = new XMLDom(res.getBody());
}
@future (callout=true)
Public static void smsCustomer1(Id tskId) {
SMS_Settings__c smsS = [select id, Name, senderId__c,task_sms__c,Mobile_Number__c, ValueFirst_Username__c, ValueFirst_Password__c from SMS_Settings__c where name ='Proposal'] ;
Task tsk = [select id,Status,Proposal_Number__r.Account_Name__r.FirstName,Proposal_Number__r.Account_Name__r.LastName,Proposal_Number__r.Account_Name__r.PersonMobilePhone
from Task where id=:tskId];
String fromNo = smsS.senderId__c; //'PGFIIT';
String toNo = '91' + tsk.Proposal_Number__r.Account_Name__r.PersonMobilePhone; // '919790882125'; //
String Name = tsk.Proposal_Number__r.Account_Name__r.FirstName + ' '+ tsk.Proposal_Number__r.Account_Name__r.LastName;
//system.debug(Name + '\n'+ppd.Paid_Amount__c+'\n'+ppd.Premium__r.Proposal_Number__c);
String message = smss.task_sms__c.replace('CUSTNAME', Name);
//message = message.replace('PAYAMOUNT', String.valueOf(ppd.Paid_Amount__c));
//message = message.replace('PROPID', ppd.Premium__r.Proposal_Number__c);
String xmlMessage = '<?xml version="1.0" encoding="ISO-8859-1"?>';
xmlMessage += '<!DOCTYPE MESSAGE SYSTEM "http://127.0.0.1/psms/dtd/message.dtd" >';
xmlMessage += '<MESSAGE><USER USERNAME="'+smss.ValueFirst_Username__c+'" PASSWORD="'+smss.ValueFirst_Password__c+'"/>';
xmlMessage += '<SMS UDH="0" CODING="1" TEXT="'+message +'" PROPERTY="0" ID="'+tskId+'">';
xmlMessage += '<ADDRESS FROM="'+ fromNo +'" TO="'+toNo +'" SEQ="1" TAG="Premium Payment" />';
xmlMessage += '</SMS></MESSAGE>';
String encodedUrl = 'http://api.myvaluefirst.com/psms/servlet/psms.Eservice2?action=send&data='+EncodingUtil.urlEncode(xmlMessage, 'UTF-8');
String decodedUrl = EncodingUtil.urlDecode(encodedUrl, 'UTF-8'); system.debug(decodedUrl);
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(encodedUrl);
req.setMethod('GET');
if (test.isRunningTest()!=true) {
HttpResponse res = invokeWebService(h, req); handleWebServiceResponseCust(tskId);
system.debug(res.getBody());
} else {
handleWebServiceResponseCust(tskId);
}
}
public static HttpResponse invokeWebService(Http h, HttpRequest req){
HttpResponse res = h.send(req); return res;
}
public static void handleWebServiceResponseCust(String tskid){
Task tsk = [select id,Status,task_sms__c,Proposal_Number__r.Account_Name__r.PersonMobilePhone from Task where id=:tskId];
if (tsk.Status == 'Completed') {
tsk.task_sms__c = true;
update tsk;
}
}
}
Test Class :
@isTest (SeeAllData=true)
private class TestTaskSMS{
static testMethod void testTaskSMS() {
test.startTest();
RecordType rt = [select id from RecordType where sObjectType = 'Account' and Name = 'Individual'];
Account a = new Account(Salutation = 'Mr', FirstName='Test', LastName='Test Acc', PersonMobilePhone='9790882125', RecordTypeId = rt.Id, House_No_Street__c = 'Address');
insert a;
Proposal_Details__c propd = new Proposal_Details__c( Account_Name__c = a.Id, Status_del__c = 'Initiated', Proposal_Application_Number__c = '001' );
insert propd;
SMS_Details__c SMS = new SMS_Details__c(Message__c = 'Test1', Mobile_Number__c = '9791222241', GUID__c = 'kgckh225103451f410014fveqh--PFPDLXML', Submited_Date_Time__c = Date.Today());
insert SMS;
SMS_Details__c SMS1 = new SMS_Details__c(Message__c = 'Test1', Mobile_Number__c = '9791222241', GUID__c = 'kgckh225103451f410014fveqh--PFPDLXML',
SMS_Request_Sent__c = TRUE, Submited_Date_Time__c = Date.Today());
insert SMS1;
SMS_Settings__c st = new SMS_Settings__c(Name='Proposal1', SenderId__c = 'PGFIIT', ValueFirst_Username__c = 'pfpdlxml', ValueFirst_Password__c = 'pfpxml@123');
insert st;
Task tsk=new Task(Status = 'Planned');
insert tsk;
tsk.Status = 'Completed';
update tsk;
SMSTask si = new SMSTask();
si.sendMessage();
OI_custom_SMS_Settings sms5 = new OI_custom_SMS_Settings ();
sms5.getsmss();
sms5.save();
test.stopTest();
}
}
Please help
Can anyone help me with code coverage.
Class:
public class SMSTask {
public string strMobNo{ get; set; }
public SMSTask(ApexPages.StandardController controller){
String MBRid;
}
public SMSTask () { }
Public void sendMessage(){
Http http = new Http();
HttpRequest req = new HttpRequest();
String fromNum='918973147939';
//String toNum= '919790882125';
strMobNo = '919790882125';
req.setEndpoint('http://www.myvaluefirst.com/smpp/sendsms?username=dempeerlesshttp&password=demo4321&to=91'+strMobNo+'&from='+fromNum+'&text=hi');
req.setMethod('GET');
if (test.isRunningTest()!=true) HttpResponse res = http.send(req);
//XMLDom responseXML = new XMLDom(res.getBody());
}
@future (callout=true)
Public static void smsCustomer1(Id tskId) {
SMS_Settings__c smsS = [select id, Name, senderId__c,task_sms__c,Mobile_Number__c, ValueFirst_Username__c, ValueFirst_Password__c from SMS_Settings__c where name ='Proposal'] ;
Task tsk = [select id,Status,Proposal_Number__r.Account_Name__r.FirstName,Proposal_Number__r.Account_Name__r.LastName,Proposal_Number__r.Account_Name__r.PersonMobilePhone
from Task where id=:tskId];
String fromNo = smsS.senderId__c; //'PGFIIT';
String toNo = '91' + tsk.Proposal_Number__r.Account_Name__r.PersonMobilePhone; // '919790882125'; //
String Name = tsk.Proposal_Number__r.Account_Name__r.FirstName + ' '+ tsk.Proposal_Number__r.Account_Name__r.LastName;
//system.debug(Name + '\n'+ppd.Paid_Amount__c+'\n'+ppd.Premium__r.Proposal_Number__c);
String message = smss.task_sms__c.replace('CUSTNAME', Name);
//message = message.replace('PAYAMOUNT', String.valueOf(ppd.Paid_Amount__c));
//message = message.replace('PROPID', ppd.Premium__r.Proposal_Number__c);
String xmlMessage = '<?xml version="1.0" encoding="ISO-8859-1"?>';
xmlMessage += '<!DOCTYPE MESSAGE SYSTEM "http://127.0.0.1/psms/dtd/message.dtd" >';
xmlMessage += '<MESSAGE><USER USERNAME="'+smss.ValueFirst_Username__c+'" PASSWORD="'+smss.ValueFirst_Password__c+'"/>';
xmlMessage += '<SMS UDH="0" CODING="1" TEXT="'+message +'" PROPERTY="0" ID="'+tskId+'">';
xmlMessage += '<ADDRESS FROM="'+ fromNo +'" TO="'+toNo +'" SEQ="1" TAG="Premium Payment" />';
xmlMessage += '</SMS></MESSAGE>';
String encodedUrl = 'http://api.myvaluefirst.com/psms/servlet/psms.Eservice2?action=send&data='+EncodingUtil.urlEncode(xmlMessage, 'UTF-8');
String decodedUrl = EncodingUtil.urlDecode(encodedUrl, 'UTF-8'); system.debug(decodedUrl);
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(encodedUrl);
req.setMethod('GET');
if (test.isRunningTest()!=true) {
HttpResponse res = invokeWebService(h, req); handleWebServiceResponseCust(tskId);
system.debug(res.getBody());
} else {
handleWebServiceResponseCust(tskId);
}
}
public static HttpResponse invokeWebService(Http h, HttpRequest req){
HttpResponse res = h.send(req); return res;
}
public static void handleWebServiceResponseCust(String tskid){
Task tsk = [select id,Status,task_sms__c,Proposal_Number__r.Account_Name__r.PersonMobilePhone from Task where id=:tskId];
if (tsk.Status == 'Completed') {
tsk.task_sms__c = true;
update tsk;
}
}
}
Test Class :
@isTest (SeeAllData=true)
private class TestTaskSMS{
static testMethod void testTaskSMS() {
test.startTest();
RecordType rt = [select id from RecordType where sObjectType = 'Account' and Name = 'Individual'];
Account a = new Account(Salutation = 'Mr', FirstName='Test', LastName='Test Acc', PersonMobilePhone='9790882125', RecordTypeId = rt.Id, House_No_Street__c = 'Address');
insert a;
Proposal_Details__c propd = new Proposal_Details__c( Account_Name__c = a.Id, Status_del__c = 'Initiated', Proposal_Application_Number__c = '001' );
insert propd;
SMS_Details__c SMS = new SMS_Details__c(Message__c = 'Test1', Mobile_Number__c = '9791222241', GUID__c = 'kgckh225103451f410014fveqh--PFPDLXML', Submited_Date_Time__c = Date.Today());
insert SMS;
SMS_Details__c SMS1 = new SMS_Details__c(Message__c = 'Test1', Mobile_Number__c = '9791222241', GUID__c = 'kgckh225103451f410014fveqh--PFPDLXML',
SMS_Request_Sent__c = TRUE, Submited_Date_Time__c = Date.Today());
insert SMS1;
SMS_Settings__c st = new SMS_Settings__c(Name='Proposal1', SenderId__c = 'PGFIIT', ValueFirst_Username__c = 'pfpdlxml', ValueFirst_Password__c = 'pfpxml@123');
insert st;
Task tsk=new Task(Status = 'Planned');
insert tsk;
tsk.Status = 'Completed';
update tsk;
SMSTask si = new SMSTask();
si.sendMessage();
OI_custom_SMS_Settings sms5 = new OI_custom_SMS_Settings ();
sms5.getsmss();
sms5.save();
test.stopTest();
}
}
Please help
The below articles give an idea of how code coverage can be improved. You will need to customize based on your requirement
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines
https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test
How to cover this portion.Please help with sample code.
@future (callout=true)
Public static void smsCustomer1(Id tskId) {
SMS_Settings__c smsS = [select id, Name, senderId__c,task_sms__c,Mobile_Number__c, ValueFirst_Username__c, ValueFirst_Password__c from SMS_Settings__c where name ='Proposal'] ;
Task tsk = [select id,Status,Proposal_Number__r.Account_Name__r.FirstName,Proposal_Number__r.Account_Name__r.LastName,Proposal_Number__r.Account_Name__r.PersonMobilePhone
from Task where id=:tskId];
String fromNo = smsS.senderId__c; //'PGFIIT';
String toNo = '91' + tsk.Proposal_Number__r.Account_Name__r.PersonMobilePhone; // '919790882125'; //
String Name = tsk.Proposal_Number__r.Account_Name__r.FirstName + ' '+ tsk.Proposal_Number__r.Account_Name__r.LastName;
String message = smss.task_sms__c.replace('CUSTNAME', Name);
String xmlMessage = '<?xml version="1.0" encoding="ISO-8859-1"?>';
xmlMessage += '<!DOCTYPE MESSAGE SYSTEM "http://127.0.0.1/psms/dtd/message.dtd" >';
xmlMessage += '<MESSAGE><USER USERNAME="'+smss.ValueFirst_Username__c+'" PASSWORD="'+smss.ValueFirst_Password__c+'"/>';
xmlMessage += '<SMS UDH="0" CODING="1" TEXT="'+message +'" PROPERTY="0" ID="'+tskId+'">';
xmlMessage += '<ADDRESS FROM="'+ fromNo +'" TO="'+toNo +'" SEQ="1" TAG="Premium Payment" />';
xmlMessage += '</SMS></MESSAGE>';
String encodedUrl = 'http://api.myvaluefirst.com/psms/servlet/psms.Eservice2?action=send&data='+EncodingUtil.urlEncode(xmlMessage, 'UTF-8');
String decodedUrl = EncodingUtil.urlDecode(encodedUrl, 'UTF-8'); system.debug(decodedUrl);
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(encodedUrl);
req.setMethod('GET');
if (test.isRunningTest()!=true) {
HttpResponse res = invokeWebService(h, req); handleWebServiceResponseCust(tskId);
system.debug(res.getBody());
} else {
handleWebServiceResponseCust(tskId);
}
}