You need to sign in to do that
Don't have an account?
Alan DePew
Test Apex Class for Visualforce Page
I have an apex class that is being called from a Visualforce Page when a user clicks a button on the Contact page. How would I go about writing a test method for this apex class?
Here is the Visualforce Page:
public class USPS_Contact { //Apex properties or variables public Id Id { get; set; } public Boolean MailingValidated; public String MailingStreet, MailingCity, MailingState, MailingZipCode; public String USPS_UID, BaseURL, ReturnValue, Endpoint, resXML; public String USPSAddress, USPSCity, USPSState, USPSZip5, USPSZip4, USPSDPV, USPSMsg, ErrorMsg; //Constructor to get the Contact record public USPS_Contact(ApexPages.StandardController controller) { controller.addFields(new String[]{ 'MailingStreet', 'MailingCity', 'MailingState', 'MailingPostalCode', 'Mailing_Address_Validated__c' }); con = (Contact)controller.getRecord(); Id = con.Id; MailingStreet = con.MailingStreet; MailingCity = con.MailingCity; MailingState = con.MailingState; MailingZipCode = con.MailingPostalCode; MailingValidated = con.Mailing_Address_Validated__c; }//End Constructor //Method that can is called from the Visual Force page action attribute public PageReference USPSVerify() { IF(MailingStreet != NULL && MailingValidated == FALSE ){ //Build your code logic here USPS_UID = 'Eradicated'; BaseURL = 'http://production.shippingapis.com/ShippingAPI.dll?API=Verify&XML='; ReturnValue = '<AddressValidateRequest USERID="' + USPS_UID + '"><Address ID="0">'; ReturnValue += '<Address1></Address1>'; ReturnValue += '<Address2>' + MailingStreet + '</Address2>'; ReturnValue += '<City>' + MailingCity + '</City>'; ReturnValue += '<State>' + MailingState + '</State>'; ReturnValue += '<Zip5>' + MailingZipCode + '</Zip5>'; ReturnValue += '<Zip4></Zip4>'; ReturnValue += '</Address></AddressValidateRequest>'; ReturnValue = EncodingUtil.urlEncode(ReturnValue, 'UTF-8'); ReturnValue = BaseURL + ReturnValue; endpoint = ReturnValue; HttpRequest request=new HttpRequest(); request.setEndPoint(endpoint); request.setMethod('GET'); request.setHeader('Content-Type', 'application/xml'); Http p=new Http(); HttpResponse response=p.send(request); resXML=response.getBody(); Dom.Document docx = new Dom.Document(); docx.load(resXML); Dom.XmlNode xroot = docx.getRootElement(); List<Dom.XmlNode> childelements = xroot.getChildElements(); //Get XML tags from response String stringchildren = string.join(childelements,','); //Convert XML tags into string Boolean result = stringchildren.contains('Error'); //Check for error tag in XML result IF(result == TRUE) { ErrorMsg = xroot.getchildElement('Address', null).getchildElement('Error', null).getchildElement('Description',null).getText(); System.debug(ErrorMsg); try { // Query for the contact, which has been associated with an account. Contact ErrorContact = [SELECT Id FROM Contact WHERE Id = :Id]; // Update the Validation Fields for Error Message ErrorContact.Mailing_Address_Validated__c = FALSE; ErrorContact.Mailing_Address_Validation_Message__c = ErrorMsg; ErrorContact.Mailing_Address_Validation_Timestamp__c = datetime.now(); // Update the related account industry update ErrorContact; } catch(Exception e) { System.debug('An unexpected error has occurred: ' + e.getMessage()); } } ELSE { try { USPSAddress = xroot.getChildElement('Address', null).getchildElement('Address2', null).getText(); USPSCity = xroot.getChildElement('Address', null).getchildElement('City', null).getText(); USPSState = xroot.getChildElement('Address', null).getchildElement('State', null).getText(); USPSZip5 = xroot.getChildElement('Address', null).getchildElement('Zip5', null).getText(); USPSZip4 = xroot.getChildElement('Address', null).getchildElement('Zip4', null).getText(); USPSMsg = 'Mailing Address Validated'; // Query for the contact, which has been associated with an account. Contact SuccessContact = [SELECT Id FROM Contact WHERE Id = :Id]; // Update the Validation Fields for Success Message SuccessContact.MailingStreet = USPSAddress; SuccessContact.MailingCity = USPSCity; SuccessContact.MailingState = USPSState; SuccessContact.MailingPostalCode = USPSZip5+'-'+USPSZip4; update SuccessContact; //We update the record twice to avoid turning off the validation fields when the address is changed. SuccessContact.Mailing_Address_Validated__c = TRUE; SuccessContact.Mailing_Address_Validation_Message__c = USPSMsg; SuccessContact.Mailing_Address_Validation_Timestamp__c = datetime.now(); update SuccessContact; } catch(Exception e) { System.debug('An unexpected error has occurred: ' + e.getMessage()); } } } //Return to the Contact Page PageReference pageRef = new PageReference('/'+Id); pageRef.setRedirect(true); return pageRef; } }
Here is the Visualforce Page:
<apex:page standardController="Contact" extensions="USPS_Contact" action="{!USPSVerify}"> <apex:form > <apex:inputHidden value="{!contact.Id}"/> </apex:form> </apex:page>
public static testMethod void testUSPSVerify()
{
PageReference pageRef = Page.[page name here];
Test.setCurrentPage( pageRef );
USPS_Contact controller = new USPS_Contact();
system.assert(controller != null); // controller has successfully been created
pageRef = controller.USPSVerify();
}