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
Bryan TelfordBryan Telford 

Apex test class - code coverage

I have the following Apex class that is only getting 57% code coverage.  Test class below as well. I feel like there is something simple that I'm missing. Any help would be appreciated!

public class taskController{

    public Project_Task__c task{get;set;}
    public List<Project_Task_Assignment__c> assignment{get;set;}
    public taskController(){
        task = new Project_Task__c();
        assignment = new List<Project_Task_Assignment__c>();
        task.Project__c = ApexPages.currentPage().getParameters().get('Id');
     
    }
    public void AddRow(){
        assignment.add(new Project_Task_Assignment__c());
    
    }
    public PageReference save(){

     try{
            insert task;
            List<Project_Task_Assignment__c> taskAssignment = new List<Project_Task_Assignment__c>();
            for(Project_Task_Assignment__c c : assignment){
                c.Project_Task__c = task.id;
                taskAssignment.add(c);
    
            }

            if(assignment != null){
                insert taskAssignment;
           }
           
            PageReference taskPage = new PageReference('/apex/manage_task?id=' + task.id);
        taskPage.setRedirect(true);
        return taskPage;
      } 
      
     catch (Exception e) {apexpages.addmessages(e);}return null;        
      
  }  
    
}

*****Test Class*****
@isTest
    public class taskControllerTest{
         private static testMethod void saveTask() {
        
         Account acc = new Account(Name = 'Sample Account'); insert acc;
         Contact con = new Contact(LastName = 'Smith', AccountId = acc.id); insert con;    
         Plan__c plan = new Plan__c(Name = 'Test'); insert plan;
         Project__c project = new Project__c(Name = 'Test', Plan__c = plan.id); insert project;
         
    
            
    
         Test.startTest();
         
         PageReference pageRef = Page.AccountContact;
         
         Project_Task__c task = new Project_Task__c(Project__c = project.id, Name = 'Test Task'); 
         insert task;
         Project_Task_Assignment__c assignment = new Project_Task_Assignment__c(Project_Task__c = task.id, Contact__c = con.id);
         insert assignment;
         Test.setCurrentPage(pageRef);
         taskController controller = new taskController();
         controller.addRow();       

         controller.save();
         pageRef.getParameters().put('id',task.id);   

         Test.stopTest();        
         
         System.assertNotEquals(task.Name, null);
         System.assertNotEquals(assignment, null);         
    
   } 
             
}
Best Answer chosen by Bryan Telford
Adarsh.SharmaAdarsh.Sharma
Hi Bryan,

I have found what is issue.Please try below code.
 
@isTest
    public class taskControllerTest{
         private static testMethod void saveTask() {
        
         Account acc = new Account(Name = 'Sample Account'); insert acc;
         Contact con = new Contact(LastName = 'Smith', AccountId = acc.id); insert con;    
         Plan__c plan = new Plan__c(Name = 'Test'); insert plan;
         Project__c project = new Project__c(Name = 'Test', Plan__c = plan.id); insert project;
         
         Test.startTest();
         Project_Task__c task = new Project_Task__c(Project__c = project.id, Name = 'Test Task'); 
         insert task;
         Project_Task_Assignment__c assignment = new Project_Task_Assignment__c(Project_Task__c = task.id, Contact__c = con.id);
         insert assignment;
         
         Project_Task__c otask = new Project_Task__c(Name = 'Test Task 1'); 
             
         PageReference pageRef = Page.AccountContact;
         pageRef.getParameters().put('Id',task.id);   
         Test.setCurrentPage(pageRef);
         
         taskController controller = new taskController();
         controller.task = otask;
         controller.addRow();       
         controller.save();

         Test.stopTest();        
         
         System.assertNotEquals(task.Name, null);
         System.assertNotEquals(assignment, null);         
    
   } 
             
}

Thanks,

All Answers

Raj VakatiRaj Vakati
Can you try below code 
 
@isTest
    public class taskControllerTest{
         private static testMethod void saveTask() {
        
         Account acc = new Account(Name = 'Sample Account'); insert acc;
         Contact con = new Contact(LastName = 'Smith', AccountId = acc.id); insert con;    
         Plan__c plan = new Plan__c(Name = 'Test'); insert plan;
         Project__c project = new Project__c(Name = 'Test', Plan__c = plan.id); insert project;
         
    
            
    
         Test.startTest();
		 
		  Project_Task__c task = new Project_Task__c(Project__c = project.id, Name = 'Test Task'); 
         insert task;
		 
		 Project_Task_Assignment__c assignment = new Project_Task_Assignment__c(Project_Task__c = task.id, Contact__c = con.id);
         insert assignment;
         
         PageReference pageRef = Page.AccountContact;
          pageRef.getParameters().put('id',task.id); 
         Test.setCurrentPage(pageRef);		  
        
		 taskController controller = new taskController();
         controller.addRow();       
		 controller.save();
 
         Test.stopTest();        
        
   } 
             
}

 
Bryan TelfordBryan Telford
Hi Raj V, 
Same thing, only getting 57% coverage (12/21 lines)

Bryan
Adarsh.SharmaAdarsh.Sharma
Hi Bryan,

Please try this
 
@isTest
    public class taskControllerTest{
         private static testMethod void saveTask() {
        
         Account acc = new Account(Name = 'Sample Account'); insert acc;
         Contact con = new Contact(LastName = 'Smith', AccountId = acc.id); insert con;    
         Plan__c plan = new Plan__c(Name = 'Test'); insert plan;
         Project__c project = new Project__c(Name = 'Test', Plan__c = plan.id); insert project;
         
         Test.startTest();
         Project_Task__c task = new Project_Task__c(Project__c = project.id, Name = 'Test Task'); 
         insert task;
         Project_Task_Assignment__c assignment = new Project_Task_Assignment__c(Project_Task__c = task.id, Contact__c = con.id);
         insert assignment;
         
         PageReference pageRef = Page.AccountContact;
         pageRef.getParameters().put('Id',task.id);   
         Test.setCurrentPage(pageRef);
         
         taskController controller = new taskController();
         controller.addRow();       
         controller.save();

         Test.stopTest();        
         
         System.assertNotEquals(task.Name, null);
         System.assertNotEquals(assignment, null);         
    
   } 
             
}

 
Bryan TelfordBryan Telford
Hi SFDom, 
I got the same result (57% coverage - 12/21 lines covered)

Bryan
Adarsh.SharmaAdarsh.Sharma
Hi Bryan,

I have found what is issue.Please try below code.
 
@isTest
    public class taskControllerTest{
         private static testMethod void saveTask() {
        
         Account acc = new Account(Name = 'Sample Account'); insert acc;
         Contact con = new Contact(LastName = 'Smith', AccountId = acc.id); insert con;    
         Plan__c plan = new Plan__c(Name = 'Test'); insert plan;
         Project__c project = new Project__c(Name = 'Test', Plan__c = plan.id); insert project;
         
         Test.startTest();
         Project_Task__c task = new Project_Task__c(Project__c = project.id, Name = 'Test Task'); 
         insert task;
         Project_Task_Assignment__c assignment = new Project_Task_Assignment__c(Project_Task__c = task.id, Contact__c = con.id);
         insert assignment;
         
         Project_Task__c otask = new Project_Task__c(Name = 'Test Task 1'); 
             
         PageReference pageRef = Page.AccountContact;
         pageRef.getParameters().put('Id',task.id);   
         Test.setCurrentPage(pageRef);
         
         taskController controller = new taskController();
         controller.task = otask;
         controller.addRow();       
         controller.save();

         Test.stopTest();        
         
         System.assertNotEquals(task.Name, null);
         System.assertNotEquals(assignment, null);         
    
   } 
             
}

Thanks,
This was selected as the best answer
Adarsh.SharmaAdarsh.Sharma
Hi Bryan,

If your problem have resolve, then mark it as a best answer.

Thanks
Bryan TelfordBryan Telford
Hi SFDom, 

I appreciate the help, but unfortunately it did not resolve the issue. Same result - stuck at 57% (12/21 lines). It is not covering the following lines: 

 List<Project_Task_Assignment__c> taskAssignment = new List<Project_Task_Assignment__c>();
            for(Project_Task_Assignment__c c : assignment){
                c.Project_Task__c = task.id;
                taskAssignment.add(c);
    
            }

            if(assignment != null){
                insert taskAssignment;
           }
Adarsh.SharmaAdarsh.Sharma
it is failing when inserting Project_Task__c record.so please check is there any required field on Project_Task__c.

Please check debug log for error.
 
Bryan TelfordBryan Telford
Hi SFDom, you are correct! It was not inserting the task because I had a master-detail relationship between Project__c and Project_Task__c.. I simply modififed this line: 
Project_Task__c otask = new Project_Task__c(Name = 'Test Task 1', Project__c = project.id);

And now all the code is covered. 

Thanks so much!
Bryan