+ Start a Discussion
Liz Gibbons 16Liz Gibbons 16 

New dev struggling with email automation and testing

Hi All,

I am writing a class to send an email follow-up when a community user hasn't logged in in 2 weeks. I think I have the class how it should be, but cannot figure out how to get the test class to cover the code.  

The class:
public class EmailFeedbackSurvey {
    public static void getFeedback(){
        
        //date calculation for soql
        date d = system.today().addDays(-14);
        
        //list for feedback survey
        User[] u = [SELECT Id, Email, Contact.Name FROM User 
                    WHERE ProfileId = '00e16000000S7BU' AND LastLoginDate = Null];
            User n = new User();
            u.add(n);
        
        List<Messaging.SingleEmailMessage> feedbacks = new List<Messaging.SingleEmailMessage>();
        
         
        for(Integer i= 0; i< u.size(); i++){
            Messaging.SingleEmailMessage feedback = new Messaging.SingleEmailMessage();
                List<String> sendTo = new List<String>();
                sendTo.add(n.Email);
                feedback.setToAddresses(sendTo);
                feedback.setReplyTo('info@digitalonramps.com');
                feedback.setSenderDisplayName('Digital On-Ramps');
                feedback.setTemplateId('00X17000000Dqey');
            
            feedbacks.add(feedback);
            
             }
        Messaging.sendEmail(feedbacks);
    }

}

The test class: 
@isTest public class EmailFeedbackSurveyTest {
    @isTest public static void emailUser(){
        Id p = [SELECT id FROM Profile WHERE name = 'Learner Profile Community User'].id;
        
        Account acct = new Account(name= 'TestAcct');
        insert acct;
        
        Contact con = new Contact(LastName = 'Learner', AccountId = acct.Id);
        insert con;
        
        User u = new User(alias = 'learner1', email = 'learner@testing.org', emailencodingkey='UTF-8', lastname='Learner', languagelocalekey='en_US',
                localesidkey='en_US', profileid = p, country='United States',IsActive =true,
                ContactId = con.Id,
                timezonesidkey='America/Los_Angeles', username='tester@noemail.com');
        insert u;
        
                
        System.runAs(u){
            EmailFeedbackSurvey test = new EmailFeedbackSurvey();
            
            
            
        }

        
    }

}

I'm pretty sure something needs to happen after 
EmailFeedbackSurvey test = new EmailFeedbackSurvey();
but I'm not sure what. I started coding last week, so I think I'm in a bit over my head with this. 
Best Answer chosen by Liz Gibbons 16
Terence_ChiuTerence_Chiu
Instead of instantiating the class object just call the static method.

EmailFeedbackSurvey test = new EmailFeedbackSurvey();
test.​getFeedback();
EmailFeedbackSurvey.getFeedback();

All Answers

Terence_ChiuTerence_Chiu
Have you tried adding the following line after instantiating the class object? You are not calling the static method getFeedback of the class.

EmailFeedbackSurvey test = new EmailFeedbackSurvey();
test.​getFeedback();
Liz Gibbons 16Liz Gibbons 16
I get an error when I do this reading: "Static methods cannot be invoked through an object instance: getFeedback()"
Terence_ChiuTerence_Chiu
Instead of instantiating the class object just call the static method.

EmailFeedbackSurvey test = new EmailFeedbackSurvey();
test.​getFeedback();
EmailFeedbackSurvey.getFeedback();
This was selected as the best answer
Liz Gibbons 16Liz Gibbons 16
That worked! Thanks!