You need to sign in to do that
Don't have an account?

Struggling on Test Class for Apex VF COntroller
Not really sure how to write this class. I cannot seem to get any coverage, and I am stumped as to why.
Here is my class:
public with sharing class MyChartController { public String strCase; public MyChartController(ApexPages.StandardController stdController){ strCase = ApexPages.currentPage().getParameters().get('Id'); } public List<AggregateResult> getDatabyType(){ Case c = [Select id, ContactId From Case where id =: strCase LIMIT 1]; List<AggregateResult> result = [Select COUNT(Id) tskCNT, Type FROM Task WHERE WhoId =: c.ContactId GROUP BY Type]; return result; } }
And here is the Test Class that has no coverage results:
@isTest private class MyChartController_Test { static testMethod void MyChartController_Test() { PageReference pageRef1 = Page.MyChart; pageRef1.getParameters().get('Id'); Test.setCurrentPage(pageRef1); Account act1 = new Account(); act1.LastName = 'Test'; act1.RecordTypeId = '012i00000006R7z'; insert act1; Contact ct1 = new Contact(); ct1.AccountId = act1.ParentId; ct1.FirstName = 'John'; ct1.LastName = 'Test'; insert ct1; Task tsk1 = new Task(); tsk1.Subject = 'Other'; tsk1.Priority = 'High'; tsk1.OwnerId = '005i0000000t0F5'; tsk1.Type = 'Follow'; tsk1.Status = 'Completed'; tsk1.WhoId = ct1.Id; insert tsk1; Case cs1 = new Case(); cs1.Origin = 'Web'; cs1.ContactId = ct1.Id; cs1.Status = 'Closed'; insert cs1; ApexPages.currentPage().getParameters().put('retURL','/'+cs1.id); MyChartController(new ApexPages.StandardController(cs1)); } }
Any assistance and guidance would be greatly appreciated.
You must be using the MyChartController as controller extension. Make sure that the MyChartController class has a parameterized constructor.
And while creating the instance of MyChartController in text method, make sure that you are passing appropriate parameter of StandardController.
Thanks,
Neeraj
All Answers
Well you're expecting a parameter called Id but in your test case you're only providing a retURL
Hi,
Try following code:
private class MyChartController_Test {
static testMethod void MyChartController_Test() {
Account act1 = new Account();
act1.Name = 'Test Account';
insert act1;
Contact ct1 = new Contact();
ct1.AccountId = act1.ParentId;
ct1.FirstName = 'John';
ct1.LastName = 'Test';
insert ct1;
Task tsk1 = new Task();
tsk1.Subject = 'Other';
tsk1.Priority = 'High';
tsk1.Type = 'Follow';
tsk1.Status = 'Completed';
tsk1.WhoId = ct1.Id;
insert tsk1;
Case cs1 = new Case();
cs1.Origin = 'Web';
cs1.ContactId = ct1.Id;
cs1.Status = 'Closed';
insert cs1;
PageReference pageRef1 = new PageReference('/apex/MyChart?id='+cs1.Id);
Test.setCurrentPage(pageRef1);
ApexPages.currentPage().getParameters().put('retURL','/'+cs1.id);
MyChartController contObj = new MyChartController(new ApexPages.StandardController(cs1));
contObj.getDatabyType();
}
}
Few things to note:
Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
Thank you,
Neeraj
The following line is erroring out with "Constructor not defined"
You must be using the MyChartController as controller extension. Make sure that the MyChartController class has a parameterized constructor.
And while creating the instance of MyChartController in text method, make sure that you are passing appropriate parameter of StandardController.
Thanks,
Neeraj