function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
newbiewithapexnewbiewithapex 

How do I create test class for Task assignment method?

I have here the helper class to create task based on custom setting. How do I write test class for this methods in main class. Here is my main
public  class PMO_Task_Assignment {
    
    public static void PMO_Task_Assignment(List<IDC_Task__c> lstTask){
        
        // Map to get task order vs. custom setting value               
        Map<String, PMO_TaskAssignment__c> alltasks = PMO_TaskAssignment__c.getAll();  
        
        // Map to get values to create defect               
        Map<String, PMO_CreateDefect__c> alldefects = PMO_CreateDefect__c.getAll();
        
        // Map to get task type vs completed IDC Task
        Map<String, IDC_Task__c> tasks = new Map<String, IDC_Task__c>();
        
        // List of new Task Types
        List<string> lstnewtasktype = new List<string>();
        
        // List of completed IDC Tasks
        List<IDC_Task__c> lstexistingTask = new List<IDC_Task__c>();
         
        // List of new IDC Tasks
        List<IDC_Task__c> lstnewTask = new List<IDC_Task__c>();
        
        // Creating a Map of Task type and completed IDC Task
        for(IDC_Task__c t: lstTask) {           
            
            if(alltasks.containskey(t.Task_Type__c)){
                
                tasks.put(PMO_TaskAssignment__c.getInstance(t.task_type__c).Task__c,t);
            }               
        }
        
        // Looping through all values in custom setting and creating list of new task types as well as completed tasks in same order
        for(PMO_TaskAssignment__c task : alltasks.values()) {
            
            if(tasks.containskey(PMO_TaskAssignment__c.getInstance(task.name).Preceding_Task__c)){
                                
                lstnewtasktype.add(PMO_TaskAssignment__c.getInstance(task.name).Task__c);
                lstexistingTask.add(tasks.get(PMO_TaskAssignment__c.getInstance(task.name).Preceding_Task__c));
            }
        }
        
        List<Defect__c> lstdefect = new List<Defect__c>(); 
        // Creating new tasks and adding them in the list
        for(integer counter=0;counter<lstnewtasktype.size();counter++){
            
            if(lstnewtasktype[counter] != system.label.PMO_Task_CodeReview || (lstnewtasktype[counter] == system.label.PMO_Task_CodeReview && lstexistingTask[counter].Build_Task_Type__c == system.label.PMO_TaskType_Customization)){
                IDC_Task__c IDCTask = new IDC_Task__c();
                //if(!TL.isEmpty())
                if(PMO_TaskAssignment__c.getInstance(lstnewtasktype[counter]).Owner_Queue__c != null){
        
                    IDCTask.OwnerId = PMO_TaskAssignment__c.getInstance(lstnewtasktype[counter]).Owner_Queue__c;
                }
                
                IDCTask.Assigned_Date__c = system.today();
                IDCTask.Task_Type__c = lstnewtasktype[counter];                      
                IDCTask.IDC_Task__c = lstexistingTask[counter].id;
                IDCTask.Track__c = lstexistingTask[counter].Track__c;
                IDCTask.Request__c = lstexistingTask[counter].Request__c;
                
                if((lstexistingTask[counter].Defect_Found__c == true && (lstexistingTask[counter].Task_Type__c.contains(system.label.PMO_Task_Review) || lstexistingTask[counter].Task_Type__c.contains(system.label.PMO_Task_Execution))) || (!lstexistingTask[counter].Task_Type__c.contains(system.label.PMO_Task_Review) && !lstexistingTask[counter].Task_Type__c.contains(system.label.PMO_Task_Execution)))
                lstnewtask.add(IDCTask); 
                
            }
            try{
                if( lstexistingTask[counter].Defect_Found__c == true && lstexistingTask[counter].Defect_Description__c != null
                           && PMO_CreateDefect__c.getInstance(lstexistingTask[counter].Task_Type__c).Create_Defect__c == true){
                   Defect__c dfct = new Defect__c();
                   Dfct.subject__c = system.label.PMO_Defect_Subject + lstexistingTask[counter].Request_Name__c;
                   Dfct.status__c = system.label.PMO_Defect_Open;
                   Dfct.Defect_Detected__c = lstexistingTask[counter].Task_Type__c;
                   Dfct.Detailed_Description__c = lstexistingTask[counter].Defect_Description__c;
                   if(lstexistingTask[counter].Request__c != null)
                        Dfct.Related_Request__c = lstexistingTask[counter].Request__c;
                   lstdefect.add(Dfct);
                  
                }  
           }
           catch(exception ex){
                 ex.setMessage(system.label.PMO_Task_Error);
           }
        }
        
        // insert the list 
        if(!lstnewtask.isEmpty()) {
            try {
                insert lstnewtask;
                insert lstdefect;
            }
            catch(DMLException ex){
                ex.setMessage(system.label.PMO_Task_Error);
            }
        }
        
   }
  
}

 
Raj VakatiRaj Vakati
@isTest
public class PMO_Task_AssignmentTest {

	@isTest 
	static void testmethod1() {
		     
		PMO_TaskAssignment__c assignement = new PMO_TaskAssignment__c();
		assignement.Name ='Test';
		assignement.Preceding_Task__c='TaskName'
		assignement.Task__c ='TaskName'; // This value need to be match task type in IDC_Task__c
		// add other valies ; 
		insert assignement ; 
		PMO_CreateDefect__c createD = new PMO_CreateDefect__c() ; 
		createD.Name = 'Test';
		insert createD ; 
		
	 IDC_Task__c tasks = new IDC_Task__c() ; 
	 tasks.task_type__c = 'TaskName';
	 // add all fields 
	 insert tasks ; 
	 // add more than one task to meet all conditions
	 
	 PMO_Task_AssignmentTest.PMO_Task_Assignment(new List<IDC_Task__c>{tasks});
	 
 
    }  
}

 
newbiewithapexnewbiewithapex
I am getting this error: "Result: [OPERATION FAILED]: classes/PMO_Task_Assignment_Test.cls: Method does not exist or incorrect signature: void PMO_Task_Assignment(List<IDC_Task__c>) from the type PMO_Task_Assignment_Test (Line: 23, Column: 28)" 
I did change name from PMO_Task_Assignment_Test.(To match the file name). Could that be a reason?
Raj VakatiRaj Vakati
My bad ..this is how it will be
 
@isTest
public class PMO_Task_AssignmentTest {

	@isTest 
	static void testmethod1() {
		     
		PMO_TaskAssignment__c assignement = new PMO_TaskAssignment__c();
		assignement.Name ='Test';
		assignement.Preceding_Task__c='TaskName'
		assignement.Task__c ='TaskName'; // This value need to be match task type in IDC_Task__c
		// add other valies ; 
		insert assignement ; 
		PMO_CreateDefect__c createD = new PMO_CreateDefect__c() ; 
		createD.Name = 'Test';
		insert createD ; 
		
	 IDC_Task__c tasks = new IDC_Task__c() ; 
	 tasks.task_type__c = 'TaskName';
	 // add all fields 
	 insert tasks ; 
	 // add more than one task to meet all conditions
	 
	 PMO_Task_Assignment.PMO_Task_Assignment(new List<IDC_Task__c>{tasks});
	 
 
    }  
}

 
newbiewithapexnewbiewithapex
I added these two methods but these doesn't really increase any code coverage. Any idea why? 
public testMethod static void testAllTriggers() {
        //Xcel_PMO_Tgr_Requirement_After
        //Xcel_PMO_Tgr_Requirement_UpdateLastTrackedChange
        
        User us = [select id from User where IsActive = true LIMIT 1];
         IDC_Task__c idcTask4 = new IDC_Task__c();
        idcTask4.Task_Type__c = 'Design';
        idcTask4.Planned_LOE__c = 1.00;
            idcTask4.OwnerId  = us.id;
        idcTask4.Start_Date__c = System.today();
        idcTask4.status__c = 'Re-assigned';
        insert idcTask4;
        idcTask4.End_Date__c = System.today();
        idcTask4.Actual_LOE__c = 1.00 ;
        idcTask4.Comments__c = 'Dev complete' ;
       
        idcTask4.status__c = 'Completed';
        update idcTask4; 
                
    }
    
    public testMethod static void testUpdateReleaseLOEOnRequestChangeTrigger() {
    
        Release__c rel = new Release__c();
        rel.Name = 'testRelease';
        insert rel;
        
        Request__c req = new Request__c();
        req.Request_Title__c = 'testRequest';
        req.Release__c = rel.Id;
        req.Req__c = 1;
        req.Des__c = 1;
        req.BUT__c = 1;
        req.SIT__c = 1;
        req.UAT__c = 1;
        req.TrnDep__c = 1;
        req.Adapt__c = 1;
        insert req;
        
        req.Req__c = 2;
        req.Des__c = 2;
        req.BUT__c = 2;
        req.SIT__c = 2;
        req.UAT__c = 2;
        req.TrnDep__c = 2;
        req.Adapt__c = 2;
        update req;
        
        delete req;
    
    }