+ Start a Discussion
Øyvind Borgersen 10Øyvind Borgersen 10 

Test class blob

Hi,

I'm creating a test class for a file creating apex class where I need some help in writing a test for the blob. Can someone help me with a test class for the following:

Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
            blob csvBlob = Blob.valueOf(finalstr);
            string csvname = 'test.csv';
            csvAttc.setFileName(csvname);
            csvAttc.setBody(csvBlob);
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            String[] toAddresses = new list <string>(); 
            toAddresses.add(emailAddress);
            String subject = 'Videogram CSV';
            //email.setsenderdisplayname ('test');
            email.setOrgWideEmailAddressId(owea.get(0).Id);
            email.setSubject(subject);
            email.setToAddresses(toAddresses);
            email.setPlainTextBody('test CSV ');
            email.setFileAttachments(new Messaging.EmailFileAttachment[] {
                csvAttc
                    });
            Messaging.SendEmailResult[] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {
                email
 
Shubham4462Shubham4462
What is the value of FinalStr here if it is a object records try to insert the record and pass the parameter in method it will automtically cover.
AnudeepAnudeep (Salesforce Developers) 
Posting here a sample test class for send email. Hope this helps
 
Global class emailHelper {
    public static void sendEmail(ID recipient, ID candidate) {
        //New instance of a single email message
        Messaging.SingleEmailMessage mail =
       new Messaging.SingleEmailMessage();
    // Who you are sending the email to
       mail.setTargetObjectId(recipient);
       // The email template ID used for the email
       //mail.setTemplateId('');
       mail.setPlainTextBody('');
       mail.setWhatId(candidate);   
       mail.setBccSender(false);
       mail.setUseSignature(false);
       mail.setReplyTo('tintu@qburst.com');
       mail.setSenderDisplayName('HR Recruiting');
       mail.setSaveAsActivity(false); 
       Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

        } 

}


@istest

public class emailHelpertest{

    public static testmethod void testvalidate(){
    
        Account newAcc = new Account();
        newAcc.name='test';
        insert newAcc;
        
        Contact con=new Contact();
        con.lastname='Testing';
        con.email='test@test.com';
        insert con;
        
        
        emailHelper.sendEmail(con.Id,newAcc.Id );
    }
}