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
RadDude89RadDude89 

Create Test Class for apex class/visualforce page

Hi,

I’m hoping someone can help me on writing a test class for an apex class that uses a visualforce page to create a registration record.  I just don’t understand how to create a test class to call a method called AddProducts() and add data to test the validation rules I have built into the apex class.

Please see below:

APEX CLASS:


Enrg_PartnerRegister_Ext_New {
public Registrations__c regObj {get; set;}
    public Register_Class(ApexPages.StandardController stdController) {
        PgId = System.currentPageReference().getParameters().get('p3_lkId');
     regObj = (Registrations__C)stdController.getRecord();
            
    }
                   public PageReference addProducts(){
        try{ 

                    if(regObj.Name__c == regObj.Legal_Name__c){
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Name can not be the same as Legal Name'));
                        return null;
                    }
                                                                           if(regObj.Cost__c == regObj.Price__c){
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Cost can not be the same as Price'));
                        return null;
                    }
                      }
}

VISUALFORCE PAGE:

<apex:page standardController="Registrations__c" Extensions="Register_Class">
  
        <apex:sectionHeader title="Registration " ></apex:sectionHeader>
        <apex:outputPanel id="error">
            <b><apex:pagemessages /></b>
        </apex:outputPanel>    
<apex:pageblockSection title="Details">
                <apex:inputField value="{!regObj.Name__c}" required="true" />
                <apex:inputField value="{!regObj.Legal_Name__c}"  />
                <apex:inputField value="{!regObj.Cost__c}" required="true"/>
                <apex:inputField value="{!regObj.Price__c}" required="true"/>
                                                            ......

So essentially, I need a test class to test attempting to create a registration record with the Name = Legal Name and Cost = Price and then allowing the records to save.
I’ve tried a number of methods but I just can’t get anything to compile on the test class.

Can someone help point me in the right direction please?

Thanks,
RadDude89

 
RadDude89RadDude89
Apologies the name of the Apex Class has been renamed to Register_Class not Enrg_PartnerRegister_Ext_New, can't seem to edit the question or delete it and re-upload with correct details!