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
Bernd NawrathBernd Nawrath 

test class for class sending mails

Good evening dear community, 
I made this code public with sharing class HelperContactTrigger {
    //static method
    public static List<Opportunity> sendEmail(List<Opportunity> opportunities) {

        //query on template object
        EmailTemplate et=[Select id from EmailTemplate where name=:'Sales: New Customer Email'];

        //list of emails
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();

        //loop
        for(Opportunity con : opportunities){

            //check for Account
            if(con.OpportunityId == null && con.Email != null){

                //initiallize messaging method
                Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();

                //set object Id
                singleMail.setTargetObjectId(con.Id);

                //set template Id
                singleMail.setTemplateId(et.Id);

                //flag to false to stop inserting activity history
                singleMail.setSaveAsActivity(false);

                //add mail
                emails.add(singleMail);
            }
        }

        //send mail
        Messaging.sendEmail(emails);

        return opportunities;
    }
}

and was about to create a proper test class, since its nessecary to make it work in the production, I'm not sure how to call the instance of this class in my tast class, any advices? I'm not sure what it exactly has to look like, I know that I need to call Accounts because that's the object but I need to know how to make it work with the method that I use in my class, its a lil different from what I usually do. Thanks in advance!
SaiGSaiG

SaTry something like this:

@isTest
private class HelperContactTriggerTest {

        Opportunity opp = new Opportunity(Name = 'TESTOpp');
        insert opp;

        List<Opportunity> o = [select id from opportunity where id=opp.id]
        test.startTest();
        HelperContactTrigger.sendEmail(o);
        test.stopTest();
}

Thanks,
Sai