+ Start a Discussion
bdstangbdstang 

Test Coverage of If Statements

I'm having a heck of a time getting code coverage on the if statements in this snippet of code from my controller.  Any help would be appreciated.

 

public AssessmentPlanController(ApexPages.StandardController controller)
{      ea=(Assessment_Plan__c)Controller.getRecord();
this.controller=Controller;

Task[] tsks = [Select t.WhoId, t.WhatId, t.Subject, t.OwnerId, t.Owner.Name, t.who.Name,t.ReminderDateTime, t.Id, t.Description, t.ActivityDate, t.AccountIdFrom Task t WHERE t.WhatId=:ea.id];
integer x = tsks.size();
if (x>0)
{
  for (Task t : tsks)
  {
    if(t.Subject=='AP: Academic / Pre-Academic ' + ea.student__r.name)
      {Task01Id=t.Id;
          if(t.who.name!=null)
            {Task01AssignedTo=t.who.name;} else
            {Task01AssignedTo=t.Owner.Name;}
       Task01Date=t.ActivityDate;}
    else if(t.Subject=='AP: Transition / Vocational ' + ea.student__r.name)  
      {Task02Id=t.Id;
          if(t.who.name!=null)
            {Task02AssignedTo=t.who.name;} else
            {Task02AssignedTo=t.Owner.Name;}
       Task02Date=t.ActivityDate;}

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bdstangbdstang

I was able to resolve this by creating a task in my controller that satisfied each of the "then" and"else" portions of the "if" statement.

All Answers

Pradeep_NavatarPradeep_Navatar

Try out the sample code given below :

 

          // This code will cover 'if' part.

            student__c st = new student__c(name="teststudent");

            insert st;

            Assessment_Plan__c asp =  new Assessment_Plan__c(name="testdata",student__c = st.id);

            insert asp;              

            Account acc = new Account(name="testaccount");

            insert acc;               

            contact con = new contact(lastname = 'testcon', AccountId=acc.id);

            insert con;                                              

           ApexPages.CurrentPage().getParameters().put('id', asp.Id);

            ApexPages.StandardController controller1 = new ApexPages.StandardController(contact);

            AssessmentPlanController concheck = new AssessmentPlanController(controller1);

           Task tsk = new Task(AccountId = acc.id,WhatId= asp.id,subject = 'AP: Academic / Pre-Academic',WhoId = con.id);

           insert tsk;

 

Hope this helps.

bdstangbdstang

Thanks Pradeep, but I've already got coverage of the "If" statement.  It's the variables under the "Then" statement I can't get coverage on.  Any thoughts?

 

if(t.Subject=='AP: Academic / Pre-Academic ' + ea.student__r.name)
      {Task01Id=t.Id;
          if(t.who.name!=null)
            {Task01AssignedTo=t.who.name;} else
            {Task01AssignedTo=t.Owner.Name;}
       Task01Date=t.ActivityDate;}
bdstangbdstang

Actually, what's not getting tested is the For loop & therefore I think it's not getting to the If statement.  Any idea how to test the For loop?

 

public AssessmentPlanController(ApexPages.StandardController controller)
{      ea=(Assessment_Plan__c)Controller.getRecord();
this.controller=Controller;

Task[] tsks = [Select t.WhoId, t.WhatId, t.Subject, t.OwnerId, t.Owner.Name, t.who.Name,t.ReminderDateTime, t.Id, t.Description, t.ActivityDate, t.AccountIdFrom Task t WHERE t.WhatId=:ea.id];
integer x = tsks.size();
if (x>0)
{
  for (Task t : tsks)
  {
    if(t.Subject=='AP: Academic / Pre-Academic ' + ea.student__r.name)
      {Task01Id=t.Id;
          if(t.who.name!=null)
            {Task01AssignedTo=t.who.name;} else
            {Task01AssignedTo=t.Owner.Name;}
       Task01Date=t.ActivityDate;}
    else if(t.Subject=='AP: Transition / Vocational ' + ea.student__r.name)  
      {Task02Id=t.Id;
          if(t.who.name!=null)
            {Task02AssignedTo=t.who.name;} else
            {Task02AssignedTo=t.Owner.Name;}
       Task02Date=t.ActivityDate;}
bdstangbdstang

Solved the For Loop issue, but still not gettingcoverage of the Then statements after the if statement.

bdstangbdstang

I was able to resolve this by creating a task in my controller that satisfied each of the "then" and"else" portions of the "if" statement.

This was selected as the best answer
orikkerorikker

What if you have 20 if then statements? There is got to be a better way...

goabhigogoabhigo

Exactly right.

 

@bdsIf you found any solution for this please do post. It will help lot of us. 

My OwnMy Own
Hi Dude, FYI.. When ever you are using SOQL in test classes use SOQL with Limit<=500. Because if your trying to moving your code to PROD It will throw you "to many SOQL" in test class(governing limits). Coming to the piece of code. 1. Edit any of your Task with t.who.name as null then Run test. sure this will pass the code coverage. or else see the in line comments in code. public AssessmentPlanController(ApexPages.StandardController controller) { ea=(Assessment_Plan__c)Controller.getRecord(); this.controller=Controller; /* Create Your list of tasks here */ // Task which will qualify your code. // Task which will not qualify your code. Task[] tsks = [Select t.WhoId, t.WhatId, t.Subject, t.OwnerId, t.Owner.Name, t.who.Name,t.ReminderDateTime, t.Id, t.Description, t.ActivityDate, t.AccountIdFrom Task t WHERE t.WhatId=:ea.id]; integer x = tsks.size(); if (x>0) { for (Task t : tsks) { if(t.Subject=='AP: Academic / Pre-Academic ' + ea.student__r.name) { Task01Id=t.Id; if(t.who.name!=null) { Task01AssignedTo=t.who.name; } else { Task01AssignedTo=t.Owner.Name; } Task01Date=t.ActivityDate; } else if(t.Subject=='AP: Transition / Vocational ' + ea.student__r.name) { Task02Id=t.Id; if(t.who.name!=null) { Task02AssignedTo=t.who.name; } else { Task02AssignedTo=t.Owner.Name; } Task02Date=t.ActivityDate; }
goabhigogoabhigo

Can you please put some line breaks? Its hard to understand what you have written.

My OwnMy Own
Sorry for that. try it now. Hi Dude, FYI.. When ever you are using SOQL in test classes use SOQL with Limit<=500. Because if your trying to moving your code to PROD It will throw you "to many SOQL" in test class(governing limits). Coming to the piece of code. 1. Edit any of your Task with t.who.name as null then Run test. sure this will pass the code coverage. or else see the in line comments in code. public AssessmentPlanController(ApexPages.StandardController controller) { ea=(Assessment_Plan__c)Controller.getRecord(); this.controller=Controller; /* Create Your list of tasks here */ // Task which will qualify your code. // Task which will not qualify your code. Task[] tsks = [Select t.WhoId, t.WhatId, t.Subject, t.OwnerId, t.Owner.Name, t.who.Name, t.ReminderDateTime, t.Id,t.Description, t.ActivityDate, t.AccountIdFrom Task t WHERE t.WhatId=:ea.id limit 450]; integer x = tsks.size(); if (x>0) { for (Task t : tsks) { if(t.Subject=='AP: Academic / Pre-Academic ' + ea.student__r.name) { Task01Id=t.Id; if(t.who.name!=null) { Task01AssignedTo=t.who.name; } else { Task01AssignedTo=t.Owner.Name; } Task01Date=t.ActivityDate; } else if(t.Subject=='AP: Transition / Vocational ' + ea.student__r.name) { Task02Id=t.Id; if(t.who.name!=null) { Task02AssignedTo=t.who.name; } else { Task02AssignedTo=t.Owner.Name; } Task02Date=t.ActivityDate; }
goabhigogoabhigo

?????????