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
Abc234Abc234 

Help on test class

public class sendEmail{
    public Account account = new Account();
    public Account acct = new Account();
    public sendEmail(ApexPages.StandardController stdController) {
        account = (Account)stdController.getRecord();
        acct = [select id,name,billingstreet,billingcity,billingstate,billingpostalcode,phone, (select Contact.Name, Contact.Email FROM Account.Contacts) from Account where id = :account.id];
    }
    public String subject { get; set; }
    public String body { get; set; }
   public Account getAccount() {
        return account;
    }

    public PageReference send() {
        // Define the email
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

        String addresses = 'bharathvsfdc@gmail.com';
        String body2;
        String[] toAddresses = addresses.split(':', 0);
        body2 = 'Name: ' + acct.Name + '<br />';
        body2 += 'Address:<br />';
        body2 += '  Street    :  ' + acct.BillingStreet + 'br />';
        body2 += '  City      :  ' + acct.BillingCity + '<br />';
        body2 += '  State     :  ' + acct.BillingState + '<br />';
        body2 += '  PostalCode:  ' + acct.BillingPostalCode + '<br />';
        body2 += 'Telphone Number:  ' + acct.Phone + '<br /><br />';
        body2 += 'Contact Name:<br />  ';
        for (Integer i = 1; i < acct.Contacts.size(); i++)
        {
            body2 += '  ' + acct.Contacts[i].Name + '<br />';
        }

        // Sets the paramaters of the email
        email.setSubject( subject );
        email.setToAddresses( toAddresses );
        email.setPlainTextBody( body2 );
        email.setHtmlBody(body2);
       
        // Sends the email
        Messaging.SendEmailResult [] r =
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});  
       
        return new PageReference('/' + account.ID);
    }
}

 

How to write test class for this

Best Answer chosen by Admin (Salesforce Developers) 
Puja_mfsiPuja_mfsi

Hi,

First you need to create the account and their contact record because you need that.after that you create standard controller object with parameter ( In your case standard controller is account Account ).

ApexPages.StandardController sc = new ApexPages.StandardController(acc);

And Then Create object of your class:

sendEmail clsObj = new sendEmail(sc);

 

 

 

@isTest
public Class TestsendEmail {

public static testMethod void unitTest() {
Account acc = new Account (
Name = 'test'
);
insert acc;
Contact con = new Contact (
accountId = acc.Id,
LastName = 'test'
);
insert con;
Contact con1 = new Contact (
accountId = acc.Id,
LastName = 'test1'
);
insert con1;
ApexPages.StandardController sc = new ApexPages.StandardController(acc);
sendEmail clsObj = new sendEmail(sc);
clsObj.getAccount();
clsObj.send();

}
}

All Answers

Puja_mfsiPuja_mfsi

Hi,

First you need to create the account and their contact record because you need that.after that you create standard controller object with parameter ( In your case standard controller is account Account ).

ApexPages.StandardController sc = new ApexPages.StandardController(acc);

And Then Create object of your class:

sendEmail clsObj = new sendEmail(sc);

 

 

 

@isTest
public Class TestsendEmail {

public static testMethod void unitTest() {
Account acc = new Account (
Name = 'test'
);
insert acc;
Contact con = new Contact (
accountId = acc.Id,
LastName = 'test'
);
insert con;
Contact con1 = new Contact (
accountId = acc.Id,
LastName = 'test1'
);
insert con1;
ApexPages.StandardController sc = new ApexPages.StandardController(acc);
sendEmail clsObj = new sendEmail(sc);
clsObj.getAccount();
clsObj.send();

}
}

This was selected as the best answer
Abc234Abc234

Thanks for your help.Now it's  92% code coverage

 

If I add code like below in PageReference send()

 

 if (acct.Name != Null)
        {
          Name = acct.Name;
        }
        if (acct.BillingStreet != Null)
        {
            Street = acct.BillingStreet;
        }

How to get the coverage for the highlighted part.

 

Thanks for the help