• Sara Marianecci
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi All,

I have been stuck in test class and not getting a way to solve the issue.
I have a VF page and controller. VF page is basically an input form where an user can fill up their information and Save.Once Save the controller will exceute and save the record to a custom object.In input form I have an input field where user can key in their email address.Upon save an email should go to the email address which has been specified in email input field.Functionality is working fine though the issue is with the test class.I understand to test email messaging we need to ensure we are inserting the correct data/record which I am doing but upon running the test class I am getting error : System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, Email address is invalid: null: [toAddresses, null]
Below is a part from my VF page:
<apex:page standardController="Driver_Survey__c" extensions="SurveyForDriverController" sidebar="false" lightningStylesheets="true">
    
    
    <apex:form > 
       
        <apex:pageBlock mode="edit" >
            <apex:pageBlockSection columns="1" collapsible="false" title="Driver Information"  >
                <apex:inputField label="Name" value="{!survey.Name__c}" required="true"/>
                <apex:inputField label="Mobile" value="{!survey.Mobile__c}" required="true" />
                <apex:inputField label="Email" value="{!survey.Email__c}" required="true" />
                	
  
            </apex:pageBlockSection>
          
            <apex:pageBlockButtons >

                <apex:commandButton value="Save" action="{!doFullSave}"/>
                  
                    <apex:commandButton value="Cancel" action="{!doCancel}" immediate="true"/>
           
            </apex:pageBlockButtons> 
        </apex:pageBlock>

    </apex:form>
</apex:page>

Below is the Save function snippets:
public class SurveyForDriverController{

    public Driver_Survey__c survey{get;set;}

    public SurveyForDriverController(ApexPages.StandardController controller){
         survey=new Driver_Survey__c();
      
    }
    public PageReference doFullSave(){
        system.debug('message1>>'+survey );
       	insert survey; 
      
  
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.toAddresses = new String[] { survey.Email__c };
            
		system.debug('message110>>'+mail.toAddresses  );
        mail.subject = 'Subject Test Message';
        //mail.plainTextBody = 'This is a test mail';
        String body = 'Hi \n  ';
       
        body += + survey.Name__c+ '<br><br>' ;
        body+= +'Request you to kindly fill in the questionnaire';
     
        mail.setHtmlBody(body);
        Messaging.SingleEmailMessage[] mailss =   new List<Messaging.SingleEmailMessage> {mail};
        Messaging.SendEmailResult[] results = Messaging.sendEmail(mailss);
         if (results[0].success) 
                {
                    System.debug('The email was sent successfully.');
                } else 
                {
                    System.debug('The email failed to send: ' + results[0].errors[0].message);
                }
       
        PageReference pageRef = new PageReference('/'+survey.Id);
        pageRef.setRedirect(true);
        return pageRef;
        }
    public PageReference doCancel(){
        PageReference pageRef = new PageReference('/');
            pageRef.setRedirect(true);
            return pageRef;
    }
   
}

Below is my test clas:
@isTest
public class TestSurveyForDriverController {

    
     static testMethod void test () {
         
       Driver_Survey__c driverSurvey=new Driver_Survey__c();
        driverSurvey.Customer_Experience__c='6';
        driverSurvey.Email__c='test@test.com';
        driverSurvey.Expected_Earnings_Weekly__c=10000;
        driverSurvey.How_long_have_you_been_working_there__c=6;
        driverSurvey.Will_you_be_planning_to_drive_for_Grab__c='Yes';
        driverSurvey.Kind_of_car_are_you_using_Rental_Car__c='Honda City';
        driverSurvey.Name='testuser1';
        driverSurvey.Name__c='testuser1';
        driverSurvey.Customer_Experience__c='All of the Above';
          insert driverSurvey;

       ApexPages.StandardController ctlr = new ApexPages.StandardController(driverSurvey);
       GrabSurveyForDriverController grabController=new GrabSurveyForDriverController(ctlr);

        grabController.doFullSave();
         grabController.doCancel();

    }
}

I am passing correct Email while creating the survey record but test class showing invalid toAddress,null

Any help would be greatly appreciated

Kindly help

Many thanks in advance​​​​​​​​​​​​​​​​​​​​​