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
Chiho SullivanChiho Sullivan 

help with apexpages.addmessage test/code coverate

been at this for hours and can't seem to figure out why my test class  isn't covering the following controller code:     } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new application.'));
      return null;
        }.

i do have a few workflow rules that are active but the initial insert of a new record would not fire any of these off...would that have an impact?

Controller is
public class KFGCreateTrainingApplicationController {

    public Applications__c applications {get; set;}
   
   // blank constructor
    public KFGCreateTrainingApplicationController() {
  applications = new Applications__c();
    }

  // save button is clicked
    public PageReference save() {
        try {
            upsert(applications); // inserts the new record into the database
        } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new application.'));
      return null;
        }
       
    // if successfully inserted new application, then displays the thank you page.
    return Page.TrainingApplicationThankYou;
    }
}


Test Is:
@isTest
public with sharing class KFGCreateTrainingAppControllerTest {
    //==================== TEST METHOD(s) ======================================
    static testMethod void KFGCreateTrainingAppControllerTest() {
     
  Applications__c newApplications = new Applications__c();
   
        // Test insert Applications__c
        newApplications.Application_Date__c = Date.Today();
        newApplications.Online_Application__c = 'Yes';
        newApplications.Class_Number_Being_Applied_For__c = 1; 
        newApplications.Applicant_First_Name__c = 'TestFirstName';
        newApplications.Applicant_Last_Name__c = 'TestLastName';
        newApplications.Age__c = 24;
        newApplications.Gender__c = 'Female';
        newApplications.Race__c = 'Latino/Hispanic';
        newApplications.Email__c = 'testreferrer@test.com';
        newApplications.State__c = 'ca';
        newApplications.Are_You_A_Current_or_Former_Foster_Youth__c = 'Yes';
        newApplications.Are_You_a_Domestic_Violence_Victim__c = 'Yes';       
        newApplications.Did_You_Attend_College__c = 'No';
        newApplications.Did_You_Attend_High_School__c = 'No';       
        newApplications.Have_You_Had_Vocational_Training__c = 'No';
        newApplications.Did_You_Serve_in_the_Armed_Forces__c = 'No';       
        newApplications.Do_You_Currently_Have_Income__c = 'No';       
        newApplications.Do_You_Have_a_History_of_Substance_Abuse__c = 'None';       
        newApplications.Do_You_Have_a_Mental_Health_Diagnosis__c = 'No';       
        newApplications.Do_You_Have_a_Physical_Disorder__c = 'No';
  newApplications.Do_You_Work_With_a_Case_Manager_Program__c = 'No';
  newApplications.How_Did_You_Hear_About_the_Program__c = 'Agency Referral';       
        newApplications.Number_of_People_You_Financially_Support__c = '1';
  newApplications.What_is_Your_Housing_Status__c = 'Rent';
        newApplications.What_is_Your_Legal_History__c = 'None';
        insert newApplications;
        KFGCreateTrainingApplicationController controller=new KFGCreateTrainingApplicationController();
        controller.save();
     
    }   
   
}
pranab khatuapranab khatua
Please add new method for Catch section 
 
static testMethod void KFGCreateTrainingAppControllerTest2() {
     
	Applications__c newApplications = new Applications__c();
   
        // Test insert Applications__c
        newApplications.Application_Date__c = Date.Today();
        
        insert newApplications;
        KFGCreateTrainingApplicationController controller=new KFGCreateTrainingApplicationController();
        controller.save();
     
    }

Thanks,
Chiho SullivanChiho Sullivan
That doesn't seem to work, code is still not covered...maybe i am doing it wrong still.  its odd cause i am using the same exact code for another object that is exactly the same except that this object has some process builder auto create record flows based on the applications that are logged.  the process is this...is the code not covering because it is also looking at process builder?

1.  applications are logged into salesforce via VF page...no fields used in process builder are needed or included in the form to log the application
2.  user reviews applications logged, when appropriate and based on statuses selected by the user, use process builder to create a student record.  none of the process builder workflow rules would be triggered when the application is created only when the record is edited to update the status
Chiho SullivanChiho Sullivan
No that didn't work.  i think it has to do with my picklist field Online_Referral__c.  i don't think my controller is inserting the default value "Yes" properly.  i've read as much as i can on the subject but i just can't seem to figure out how to make this happen....thinking because i am not calling the list properly??