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
TechEd_ProgrammerTechEd_Programmer 

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.

Best Answer chosen by Admin (Salesforce Developers) 
Neeraj_ChauriyaNeeraj_Chauriya

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

Alex.AcostaAlex.Acosta

Well you're expecting a parameter called Id but in your test case you're only providing a retURL

Neeraj_ChauriyaNeeraj_Chauriya

Hi,

 

Try following code:

 

 

@isTest
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:

 

  1. Avoid using hardcoded Ids in the test class.
  2. LastName is not a field in account object.
  3. Create PageReference with appropriate url.

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

TechEd_ProgrammerTechEd_Programmer

The following line is erroring out with "Constructor not defined"

 

MyChartController contObj = new MyChartController(new ApexPages.StandardController(cs1));
        contObj.getDatabyType();

 

Neeraj_ChauriyaNeeraj_Chauriya

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

This was selected as the best answer