You need to sign in to do that
Don't have an account?
Test Class for Trigger
H,
how do i write test class for the below trigger..I have nevr written test class..plz help
trigger CloneTemplateTasks on Project__c(after Insert) {
List<Project__c> lstPrj = new List<Project__c>();//create an empty list of Project Set<String> setType = new Set<String>();//Set for Storing Project type Set<Id> setPrjId = new Set<Id>(); // Set for Storing Projectids List<Task__c> insertlst = new list<Task__c>(); // List of Task to insert
for(Project__c proj : Trigger.new){
setType.add(proj.Project_Type__c);
}
System.debug('setType----->'+setType);
List<Project_Template__c> lstTemplate = [SELECT Template__c FROM Project_Template__c where Template__c !=null and Project_Type__c in:setType];
System.debug('lstTemplate----->'+lstTemplate);
for(Project_Template__c tempObj : lstTemplate){
setPrjId.add(tempObj.Template__c);
}
System.debug('setPrjId----->'+setPrjId);
List<Task__c> lsttask = [select id,Name,Project__c,Actual_completion_date__c,TQL_Phase__c,Key_TQL_Milestone__c,Comments__c,Owner__c,Target_date__c from Task__c where Project__c in :setPrjId ];
System.debug('lsttask ----->'+lsttask);
for(Project__c proj : Trigger.new) {
for(Task__c t : lsttask) { Task__c tsk = new Task__c(); tsk = t.clone(); tsk.Project__c = proj.id; insertlst.add(tsk); }
}
System.debug('insertlst----->'+insertlst);
insert insertlst;
}
for(Task__c t : lsttask) { Task__c tsk = new Task__c(); tsk = t.clone(); tsk.Project__c = proj.id; insertlst.add(tsk); }//need to cover this in test class
All Answers
Hi Irish,
Please try below :-
@IsTest(SeeAllData=true)
public class CloneTemplateTasks_test{
static testmethod void MyUnitTest(){
Project__c pr = new Project__c(Name='Test',-------------) ;//put the values for all the required fields to insert a record
insert pr;
}
}
for(Task__c t : lsttask) { Task__c tsk = new Task__c(); tsk = t.clone(); tsk.Project__c = proj.id; insertlst.add(tsk); }//need to cover this in test class
Irish,
You need to check if you have the populated the list lsttask or not.If you have som value in lsttask i.e if lsttask.size()>0 ,then you can cover these lines.