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
Michael MMichael M 

Test code for basic controller extension

Hello, would someone be able to show me how to write test code for this basic controller extension:

public class CampaignEmailHistoryExtension {
public final Campaign thisCampaign;
 
    Public List<ListEmail> listEmails {get; set;}
 
    Public CampaignEmailHistoryExtension(ApexPages.StandardController stdController){
        this.thisCampaign = (Campaign)stdController.getRecord();

        try{
  
            Campaign camp=[Select Id, Name from Campaign where Id = :this.thisCampaign.id];
        
        listEmails = [SELECT ID, campaignId, CreatedDate, CreatedById, CreatedBy.FirstName, CreatedBy.LastName, subject, LastViewedDate
                      FROM ListEmail 
                      WHERE  campaignId IN (Select id from Campaign where Id = :this.thisCampaign.id)];
        }
        catch(exception e){
            Apexpages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.FATAL, 'No old records'));
        }       
}
}
ANUTEJANUTEJ (Salesforce Developers) 
Hi Michael,

https://salesforce.stackexchange.com/questions/167073/how-to-cover-test-class-for-apex-controller

You can use the above link that has an implementation of an apex controller that you can check.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Michael MMichael M
Hi Anutej,

I wrote some test code, but am getting this error: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, There exists no recipient to which you can send this List Email to.: []

Any idea how to solve that? 

Here is my code:

@isTest
public class CampaignEmailHistoryTest {
    @isTest
    static void testNotesExtension(){

Campaign testCampaign= new Campaign(Name= 'Test');
insert testCampaign;
        
ListEmail le = new ListEmail(campaignId=testCampaign.id, subject= 'hey there',Status= 'Scheduled'); 
    insert le;
   
  ApexPages.StandardController con = new ApexPages.StandardController(testCampaign);
  CampaignEmailHistoryExtension  dce = new CampaignEmailHistoryExtension(con);
         
    }
    
}