+ Start a Discussion
Irish@accIrish@acc 

test Class need help

I have aritten a test class for my code..

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); // code is not covering this line

 }

 System.debug('setPrjId----->'+setPrjId);

 List<Task__c> lsttask = [select id,Name,Project__c,TQL_Phase__c,Track_Root_Cause__c,Key_TQL_Milestone__c,Owner__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();// my code is not covering these line      tsk = t.clone();      tsk.Project__c = proj.id;      insertlst.add(tsk);       }

  }

   System.debug('insertlst----->'+insertlst);

   insert insertlst;

 }

 

---------------------------------test class-----------------------------------

 

@IsTest

public class testClassCloneTemplateTasks{

 

    static testmethod void MyUnitTest(){

 

     //Create test profile 
Profile p = [select id from profile where name='System Administrator'];
Profile p2 = [select id from profile where name='TA Dev Lead'];

//Create Users 
    User User1 = new User(alias = 'u1', email='u1@testorg.com',
      emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
      localesidkey='en_US',profileid = p.Id, country='United States',
      timezonesidkey='America/Los_Angeles', username='u1@testorg.com');
    insert User1;
    
//create user2
User devlead = new User(alias = 'u2', email='u2@testorg.com',
      emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
      localesidkey='en_US', profileid = p2.Id, country='United States',
      timezonesidkey='America/Los_Angeles', username='dev@testorg.com');
    insert devlead;
    
//craete devlead user
User devlead2 = new User(alias = 'u2', email='u22@testorg.com',
      emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
      localesidkey='en_US', profileid = p2.Id, country='United States',
      timezonesidkey='America/Los_Angeles', username='dev2@testorg.com');
    insert devlead2;
//create FootPrint

Presence__c pr=new Presence__c();
pr.name='Test presence';
pr.MegaBU__c='CF';
pr.Region__c='LA';

//create TouchPoints
Channel__c ch=new Channel__c();

ch.name='test channel';
ch.type__c='Media';
ch.Presence_Id__c=pr.id;
ch.Start_Date__c=system.today();
ch.Audience__c='B2B';
ch.MegaBU__c='CF';
ch.GBU__c='CF';
ch.Region__c='LA';

  Project__c prj=new Project__c();

  prj.name='Test';
  prj.Project_Type__c='Social';   
  prj.Touchpoint_name__c=ch.id;
  prj.Overall_Project_Status__c='Not Started';
  prj.Project_Tql_Phase__c='3.build';    
  prj.Dev_Lead_User__c=devlead.id;
  insert prj;
//create Tak with required fields
task__c tsk=new Task__c();
tsk.Name='test Task';
tsk.Project__c=prj.id;
tsk.owner__c='DPDL';
tsk.Status__c='Not Started';
tsk.TQL_Phase__c='3. Build';
tsk.Actual_completion_date__c=system.today();
tsk.Target_Date_current_baseline__c=system.today();
tsk.Target_Date_original_baseline__c=system.today();
tsk.Track_Root_Cause__c=false;
tsk.Key_Tql_Milestone__C=true;

//tsk.Apex_Code__c=1;

insert tsk;

Project_Template__c tem=new Project_Template__c();
tem.Template__c=prj.id;
tem.Name='Test Template';
tem.Project_Type__c='Social';
insert tem;
 
task__c tsk1=new Task__c();
tsk1=tsk;
update tsk1;
}

 

 

 

}

 please help

Naidu PothiniNaidu Pothini
@IsTest

public class testClassCloneTemplateTasks
{
  static testmethod void MyUnitTest()
  {
    //Create test profile 
    Profile p = [select id from profile where name='System Administrator'];
    Profile p2 = [select id from profile where name='TA Dev Lead'];

    //Create Users 
    
    User User1 = new User(alias = 'u1', email='u1@testorg.com', emailencodingkey='UTF-8',
                          lastname='Testing', languagelocalekey='en_US', localesidkey='en_US',
                          profileid = p.Id, country='United States', timezonesidkey='America/Los_Angeles',
                          username='u1@testorg.com');
    insert User1;
    
    //create user2
    User devlead = new User(alias = 'u2', email='u2@testorg.com', emailencodingkey='UTF-8',
                            lastname='Testing', languagelocalekey='en_US', localesidkey='en_US',
                            profileid = p2.Id, country='United States', timezonesidkey='America/Los_Angeles',
                            username='dev@testorg.com');
    insert devlead;
      
    //craete devlead user
    User devlead2 = new User(alias = 'u2', email='u22@testorg.com', emailencodingkey='UTF-8', 
                             lastname='Testing', languagelocalekey='en_US', localesidkey='en_US',
                             profileid = p2.Id, country='United States', timezonesidkey='America/Los_Angeles',
                             username='dev2@testorg.com');
    insert devlead2;
    //create FootPrint

    Presence__c pr=new Presence__c();
    pr.name='Test presence';
    pr.MegaBU__c='CF';
    pr.Region__c='LA';

    //create TouchPoints
    Channel__c ch=new Channel__c();

    ch.name='test channel';
    ch.type__c='Media';
    ch.Presence_Id__c=pr.id;
    ch.Start_Date__c=system.today();
    ch.Audience__c='B2B';
    ch.MegaBU__c='CF';
    ch.GBU__c='CF';
    ch.Region__c='LA';

    Project__c prj=new Project__c();

    prj.name='Test';
    prj.Project_Type__c='Social';   
    prj.Touchpoint_name__c=ch.id;
    prj.Overall_Project_Status__c='Not Started';
    prj.Project_Tql_Phase__c='3.build';    
    prj.Dev_Lead_User__c=devlead.id;
    insert prj;

    Project_Template__c tem=new Project_Template__c();
    tem.Template__c=prj.id;
    tem.Name='Test Template';
    tem.Project_Type__c='Social';
    insert tem;

    //create Tak with required fields
    task__c tsk=new Task__c();
    tsk.Name='test Task';
    tsk.Project__c = prj.id;
    tsk.owner__c='DPDL';
    tsk.Status__c='Not Started';
    tsk.TQL_Phase__c='3. Build';
    tsk.Actual_completion_date__c=system.today();
    tsk.Target_Date_current_baseline__c=system.today();
    tsk.Target_Date_original_baseline__c=system.today();
    tsk.Track_Root_Cause__c=false;
    tsk.Key_Tql_Milestone__C=true;

    //tsk.Apex_Code__c=1;

    insert tsk;  /// You need to create a template before creating a task...
  }
}

 Try this...