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:pageBlockButtons>
<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}" />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>