I am new to apex and salesforce development, would like to create a test method for the controller below, however I am unable to get better results than 30% coverage and even the getter does not go through the validation. Could somebody help or provide some guidance? The controller is the following:

public class MarketingContactUpdateController {

private final MarketingContact__c marketingcontact;

 public MarketingContactUpdateController() {
 ID currentPageId = ApexPages.currentPage().getParameters().get('id');
 marketingcontact = [SELECT Id, Email__c,Phone__c,MobilePhone__c,First_Name__c,Last_Name__c,Title__c,Salutation__c,MarketingContact__c.Mailing_Address__c FROM MarketingContact__c
               WHERE Id = :currentPageId];

 public MarketingContact__c getMarketingContact() {
    return marketingcontact;

  // save button is clicked
 public PageReference save() {

try {
  update marketingcontact; // updates the record into the database
} catch (DMLException e) {
  ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error updating the contact.'));
  return null;

// if successfully updated, then displays the thank you page.
return Page.Marketing_Create_Thankyou;


And the visualforce page is the following:

    <apex:page controller="MarketingContactUpdateController">  
    <link rel="icon" type="image/png" href="{!$Resource.Favicon}" />    
    <apex:sectionHeader title="Please update your contact information"/>

    <apex:form >
    <apex:pageMessages /> <!-- this is where the error messages will appear -->
    <apex:pageBlock title="Contact Information">

    <apex:pageBlockButtons >
    <apex:commandButton action="{!save}" value="Save"/>

    <apex:pageBlockSection showHeader="false" columns="2">
    <apex:inputField value="{!marketingcontact.First_Name__c}" />
    <apex:inputField value="{!marketingcontact.Last_Name__c}" />
    <apex:inputField value="{!marketingcontact.Salutation__c}" />
    <apex:inputField value="{!marketingcontact.Title__c}" />
    <apex:inputField value="{!marketingcontact.Phone__c}" />
    <apex:inputField value="{!marketingcontact.MobilePhone__c}" />
    <apex:inputField value="{!marketingcontact.Email__c}" />
    <apex:inputField value="{!marketingcontact.Mailing_Address__c}" />           


Do some Googling and read what you find such as Testing Custom Controllers and Controller Extensions.

Some basic (untested) code to get you going:

private class MarketingContactUpdateControllerTest {

    static void happyPath() {

        MarketingContact__c mc = new MarketingContact__c(
                First_Name__c = 'Jane',
                Last_Name__c = 'Doe',
        insert mc;

        ApexPages.currentPage().getParameters().put('id', mc.Id);
        MarketingContactUpdateController c = new MarketingContactUpdateController();

        MarketingContact__c controllerMc = c.getMarketingContact();
        System.assertEquals(mc.Id, controllerMc.Id);

        controllerMc.First_Name__c = 'John';
        PageReference pr = c.save();
        System.assertNotEquals(null, pr);
        System.assertEquals('John', [
                select First_Name__c
                from MarketingContact__c
                where Id = :mc.Id

    // Add more test methods for other conditions
  • Dear Keith, thank you very much for the great answer, with your help I was able to get 100% coverage after some modifications and adding another path. – New2SFDC May 18 '17 at 8:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.