You need to sign in to do that
Don't have an account?
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);
}
}
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);
}
}
I have found what is issue.Please try below code.
Thanks,
All Answers
Same thing, only getting 57% coverage (12/21 lines)
Bryan
Please try this
I got the same result (57% coverage - 12/21 lines covered)
Bryan
I have found what is issue.Please try below code.
Thanks,
If your problem have resolve, then mark it as a best answer.
Thanks
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;
}
Please check debug log for error.
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