You need to sign in to do that
Don't have an account?
bdstang
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;}
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
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.
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?
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?
Solved the For Loop issue, but still not gettingcoverage of the Then statements after the if statement.
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.
What if you have 20 if then statements? There is got to be a better way...
Exactly right.
@bdstang: If you found any solution for this please do post. It will help lot of us.
Can you please put some line breaks? Its hard to understand what you have written.
?????????