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}" />           


closed as off-topic by Eric, martin, Raul, Ratan Paul, battery.cord May 18 '17 at 13:26

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – Eric, martin, Raul, Ratan Paul, battery.cord
If this question can be reworded to fit the rules in the help center, please edit the question.


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.