You need to sign in to do that
Don't have an account?
Begginer
Help me with writing the test class
Hi all,
Im new to salesforce and currently trying to understand test classes. could anyone help me with the test class for below code.
I wrote a bit of test code and unable to complete the code coverage.
Thanks in Advance.
global class batchcs1 implements Schedulable {
global void execute(SchedulableContext sc) {
List<Task> tskLst = [select id,status,ActivityDate,whatid,what.name from Task where what.type='Opportunity' and What.recordType.name in ('type1', 'type2') and subject like 'test Value%' and status != 'Lost' and ActivityDate >: Date.today().addDays(30)];
if(tskLst != null && !tskLst.isEmpty())
{
List<batchs__c> emailLst = [select id,Emails__c from batchs__c];
List<string> toAddressLst = new List<string>();
if(emailLst != null && emailLst.size() >0)
toAddressLst = emailLst[0].Emails__c.split(',');
DateTime d = Date.Today()+1;
String dateStr = d.format('MM/dd/yyyy') ;
List<Messaging.SingleEmailMessage> msgLst = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
semail.setToAddresses(toAddressLst);
semail.setSubject(' Reminder Test ');
String htmlBody = '<html><body> Date :'+dateStr+' <br/> <p>This is a reminder :</p> <br/> tblStr <br/><p></p><br/></body></html>';
String table = '<table border="1" width="60%"><tr style="background-color:#4BACC6;color:white;"><td width="20%" align="center">Opportunity Name</td><td width="14%" align="center">Due Date</td></tr>';
for(Task tsk : tskLst)
{
DateTime dt1 = tsk.ActivityDate+1;
table += '<tr style="background-color: #D8D8D8">';
table += '<td align="left"><a href="'+URL.getSalesforceBaseUrl().toExternalForm() + '/' +tsk.whatid+'">'+tsk.what.name+'</a></td>';
table += '<td align="left">' + dt1.format('MM/dd/yyyy') + '</td>';
table += '</tr>';
}
table = table + '</table>';
htmlBody = htmlBody.replace('tblStr', table);
semail.setHtmlBody(htmlBody);
msgLst.add(semail);
if(msgLst != null && !msgLst.isEmpty())
Messaging.sendEmail(msgLst);
}
}
}
--------------------------------------------------------------------------------------
@isTest
private class batchcs1test
{
public static testmethod void TestOppurtunity()
{
RecordType rec = [select id,name from RecordType where name = 'Type1'];
RecordType rec = [select id,name from RecordType where name = 'Type2'];
Opportunity opty = new Opportunity(account='test',name='test oppty', SalesStage__c='Closed',status__c = 'Expired',closedate=System.Today());
insert opty;
test.startTest();
batchcs1 mar = batchcs1 new ();
String sch = '0 0 13 30 11 ?';
system.schedule('Scheduled Job', sch, mar);
test.stopTest();
}
Im new to salesforce and currently trying to understand test classes. could anyone help me with the test class for below code.
I wrote a bit of test code and unable to complete the code coverage.
Thanks in Advance.
global class batchcs1 implements Schedulable {
global void execute(SchedulableContext sc) {
List<Task> tskLst = [select id,status,ActivityDate,whatid,what.name from Task where what.type='Opportunity' and What.recordType.name in ('type1', 'type2') and subject like 'test Value%' and status != 'Lost' and ActivityDate >: Date.today().addDays(30)];
if(tskLst != null && !tskLst.isEmpty())
{
List<batchs__c> emailLst = [select id,Emails__c from batchs__c];
List<string> toAddressLst = new List<string>();
if(emailLst != null && emailLst.size() >0)
toAddressLst = emailLst[0].Emails__c.split(',');
DateTime d = Date.Today()+1;
String dateStr = d.format('MM/dd/yyyy') ;
List<Messaging.SingleEmailMessage> msgLst = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
semail.setToAddresses(toAddressLst);
semail.setSubject(' Reminder Test ');
String htmlBody = '<html><body> Date :'+dateStr+' <br/> <p>This is a reminder :</p> <br/> tblStr <br/><p></p><br/></body></html>';
String table = '<table border="1" width="60%"><tr style="background-color:#4BACC6;color:white;"><td width="20%" align="center">Opportunity Name</td><td width="14%" align="center">Due Date</td></tr>';
for(Task tsk : tskLst)
{
DateTime dt1 = tsk.ActivityDate+1;
table += '<tr style="background-color: #D8D8D8">';
table += '<td align="left"><a href="'+URL.getSalesforceBaseUrl().toExternalForm() + '/' +tsk.whatid+'">'+tsk.what.name+'</a></td>';
table += '<td align="left">' + dt1.format('MM/dd/yyyy') + '</td>';
table += '</tr>';
}
table = table + '</table>';
htmlBody = htmlBody.replace('tblStr', table);
semail.setHtmlBody(htmlBody);
msgLst.add(semail);
if(msgLst != null && !msgLst.isEmpty())
Messaging.sendEmail(msgLst);
}
}
}
--------------------------------------------------------------------------------------
@isTest
private class batchcs1test
{
public static testmethod void TestOppurtunity()
{
RecordType rec = [select id,name from RecordType where name = 'Type1'];
RecordType rec = [select id,name from RecordType where name = 'Type2'];
Opportunity opty = new Opportunity(account='test',name='test oppty', SalesStage__c='Closed',status__c = 'Expired',closedate=System.Today());
insert opty;
test.startTest();
batchcs1 mar = batchcs1 new ();
String sch = '0 0 13 30 11 ?';
system.schedule('Scheduled Job', sch, mar);
test.stopTest();
}
Error MessageSystem.QueryException: List has more than 1 row for assignment to SObject
Stack TraceClass.batchcs1test.TestOppurtunity: line 5, column 1
The issue you've posted is all about this statement: a) I don't know why do you need to select record type and redefine variable rec
b) Even if you do you should do something like this (still can throw an error with index out of bounds):
Also, please notice in you scheduled job there's an logic based on Tasks, and you have to insert some tasks to have 100% coverage
Thanks
George