You need to sign in to do that
Don't have an account?
![@rajeshdulhai @rajeshdulhai](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
Help for writing test class
Hi all,
i have an apex class ,
global class TwilioWeeklyScheduleSMS Implements Schedulable
{
global void execute( SchedulableContext sc )
{
ScheduledSMS();
}
public void ScheduledSMS()
{
String message = 'SMS messages were sent to the following accounts:\n';
List<Account> acc = [SELECT Id, Name, Mobile__c from Account where Weekly_Scheduled_SMS__c = true];
if ( acc.isEmpty() ) return;
Set<String> sendToPhones = new Set<String>();
for ( Account a: acc )
{
sendToPhones.add( a.Mobile__c );
message += a.Name + '\n';
}
TwilioMessageHelper.sendSMSMessage( sendToPhones, 'Test SMS' );
List<SMS_History__c> list_SMS_Histories = new List<SMS_History__c>();
for ( Account a: acc )
{
list_SMS_Histories.add
( new SMS_History__c
( Account__c = a.Id,
Message__c = 'Test SMS ',
Mobile_Number__c = a.Mobile__c
)
);
}
insert list_SMS_Histories;
Messaging.SingleEmailMessage emailMsg = new Messaging.SingleEmailMessage();
emailMsg.setToAddresses( new List<String>{'rajesh.dulhani@nanostuffs.com' } );
emailMsg.setSenderDisplayName( 'TwilioSchedule_Weekly_SMS' );
emailMsg.setSubject( 'SMS Messages Sent' );
emailMsg.setPlainTextBody( message );
Messaging.sendEmail( new Messaging.SingleEmailMessage[] { emailMsg } );
}
}
i have written the following test code ,
@istest(seeAllData=false)
public class TestTwilioWeeklyScheduledSMS {
private static testmethod void TestTwilioWeeklyScheduledSMS (){
List<Account> acc = [SELECT Id, Name, Mobile__c from Account where Weekly_Scheduled_SMS__c = true];
if ( acc.isEmpty() ) return;
TwilioWeeklyScheduleSMS sms = new TwilioWeeklyScheduleSMS ();
sms.ScheduledSMS();
}
}
i run this test it passed but still the code coverage is 0 % . What should i modify in this test class . please help
Thanks
@istest(seeAllData=false)
public class TestTwilioWeeklyScheduledSMS {
public static testmethod void TestTwilioWeeklyScheduledSMS (){
List<Account> acc = new list<Account>();
Account Acc1 = new Account(name = 'Acc1',Weekly_Scheduled_SMS__c = true);
acc.add(Acc1);
Account Acc2 = new Account(name = 'Acc2',Weekly_Scheduled_SMS__c = true);
acc.add(Acc2);
Insert acc;
if ( acc.isEmpty() ) return;
TwilioWeeklyScheduleSMS sms = new TwilioWeeklyScheduleSMS ();
sms.ScheduledSMS();
List<SMS_History__c> list_SMS_Histories = new List<SMS_History__c>();
SMS_History1 = new SMS_History__c(Account__c = acc[0].Id,Message__c = 'Test SMS ', Mobile_Number__c = acc[0].Mobile__c);
list_SMS_Histories.add(SMS_History1);
SMS_History2 = new SMS_History__c(Account__c = acc[1].Id,Message__c = 'Test SMS ', Mobile_Number__c = acc[1].Mobile__c);
list_SMS_Histories.add(SMS_History2);
Insert list_SMS_Histories;
}
}
Regards
Sagarika Rout
SFDC Developer
No its not working
Hi all ,
I have written test class for the above code ,
@istest(seeAllData=false)
private class TestTwilioWeeklyScheduledSMS {
public static testmethod void TestTwilioWeeklyScheduledSMS()
{
Test.StartTest();
TwilioWeeklyScheduleSMS sh1 = new TwilioWeeklyScheduleSMS();
String sch='0 0 * * * ?';
system.schedule('Test check', sch, sh1);
Test.stopTest();
}
}
this test class is giving me 28% of code coverage . can you help me out what should i add more into the code to get atleast 75 % of code coverage
Thanks
I imagine you have 0 code coverage for your account list because you have not inserted an account for testing. You need to insert an account to test. Use the code that Sagarika provided to establish a list of accounts. You can also insert one account like this.
account a = new account(Name = 'Test', Phone = '4107846654', email = '123@test.com', Weekly_Scheduled_SMS__c = true);
insert a;
Adding an account or list of accounts should give you some more code coverage.
hi,
go for this link, it surely help you out
http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html