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
snapssnaps 

How do I test my outbound sendEmail() class?

I have been stuck on this task for awhile now, so any feedback would be appreciated. Here is my sendEmail() class:

 

public class sendEmail {

    public String subject { get; set; }
    public String body { get; set; }
    public Id recordId;
    private final Opportunity opportunity;

    public sendEmail() {
        recordId = ApexPages.currentPage().getParameters().get('id');
        opportunity = [SELECT Name, Ashoka_Website_Profile_Link__c,
            (SELECT Engagement_Role__c.Name,
                Engagement_Role__c.Contact__r.Email, 
                Engagement_Role__c.Contact__r.Name,
                Engagement_Role__c.Contact__r.Account.Name,
             	Engagement_Role__c.Contact__r.Id
            FROM Opportunity.R00NR0000000UWbWMAW WHERE Role__c='Nominator' AND Engagement_Role__c.Contact__r.Email != NULL ) 
            FROM Opportunity WHERE Id = :recordId];
    }

    public Opportunity getOpportunity() {
        return opportunity;
    }

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

        String addresses;
        
        // Checks to see if there even exists a contact and email address before looping 
        // through the whole list of contacts and emails 
        if (opportunity.R00NR0000000UWbWMAW[0].Contact__r.Email != null) {
            addresses = opportunity.R00NR0000000UWbWMAW[0].Contact__r.Email;
            
            for (Integer i = 1; i < opportunity.R00NR0000000UWbWMAW__r.size(); i++) {
                if (opportunity.R00NR0000000UWbWMAW[i].Contact__r.Email != null) {          
                    addresses += ':' + opportunity.R00NR0000000UWbWMAW[i].Contact__r.Email;
                }
            }
        }

        String[] toAddresses = addresses.split(':', 0);

        // Sets the parameters of the email
        email.setSubject( subject );
        email.setToAddresses( toAddresses );
        email.setPlainTextBody( body );
        email.setBccSender (true);
        
        
        Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });

        PageReference sentTrue = new PageReference('/apex/emailSentTrue?Id=' + opportunity.id); 
        sentTrue.setRedirect(true); 
        return sentTrue;
        
    }
}

 

And here is my test class:

 

 

@isTest(SeeAllData=true)

private class testSendEmail {
    private static testMethod void testSend() {
    	
        sendEmail se = new sendEmail();
        
        Opportunity testOpp = new Opportunity(name='Test Name', Ashoka_Website_Profile_Link__c = 'Some Link');
		insert testOpp;
        
     	RecordType r = [SELECT ID from RecordType WHERE SObjectType ='Account' AND Name = 'Individual']; 
        Account acc = new Account(Name='Test Account', RecordType = r);
        insert acc;       
        Account a = [SELECT ID From Account WHERE Name = 'Test Account'];
        Contact con = new Contact(FirstName='Test',LastName='Contact', Email='test@ashoka.org', Account=a);
        insert con;
        Engagement_Role__c er = new Engagement_Role__c(Contact__c = con.Id, Opportunity__c= testOpp.Id, Role__c='Nominator');
        insert er;
             
        Opportunity updatedOpp = [SELECT Name, Ashoka_Website_Profile_Link__c,
            (SELECT Engagement_Role__c.Name,
                Engagement_Role__c.Contact__r.Email, 
                Engagement_Role__c.Contact__r.Name,
                Engagement_Role__c.Contact__r.Account.Name,
                Engagement_Role__c.Contact__r.Id
            FROM Opportunity.R00NR0000000UWbWMAW WHERE Role__c='Nominator' AND Engagement_Role__c.Contact__r.Email != NULL ) 
            FROM Opportunity WHERE Id = :testOpp.Id];
       
        System.assertEquals('Test Name', updatedOpp.Name);
        System.assertEquals('Some Link', updatedOpp.Name);
        System.assertEquals('test@ashoka.org', updatedOpp.R00NR0000000UWbWMAW[0].Contact__r.Email);
      	System.assertEquals('Test Contact', updatedOpp.R00NR0000000UWbWMAW[0].Contact__r.Name);
      	System.assertEquals(con.Id, updatedOpp.R00NR0000000UWbWMAW[0].Contact__r.Id);
        
        String nextPage = se.send().getUrl();
        PageReference page = se.send();
        System.assertEquals(':testOpp.Id', nextPage); 
        
        }
}

 

My coverage is 26%, and only covers the first line of my query. What the heck am I doing wrong?

Avidev9Avidev9

It seems like you never sent the url param that you are using to do the query

 

Try something like this

Opportunity testOpp = new Opportunity(name='Test Name', Ashoka_Website_Profile_Link__c = 'Some Link'); 
insert testOpp;
PageReference pageRef = Page.YOURPAGENAME; Test.setCurrentPage(pageRef); ApexPages.currentPage().getParameters().put('id',testOpp.Id);
SendEmail se = SendEmail();

 

snapssnaps

Thank you for your response. I tried your suggestion but unfortunately my code coverage went down to 0%. I tried inserting this:

 

Test.setCurrentPageReference(new PageReference('Page.sendEmailPage')); 
System.currentPageReference().getParameters().put('id', testOpp.Id);
ApexPages.currentPage().getParameters().put('id',testOpp.Id);

 But that didn't work. So I tried using the page with a specific ID:

 

Test.setCurrentPageReference(new PageReference('/apex/sendEmailPage?id=0064000000BKcY9')); 		System.currentPageReference().getParameters().put('id', testOpp.Id);
ApexPages.currentPage().getParameters().put('id',testOpp.Id);

 Not surprisingly, that didn't work either.

Avidev9Avidev9

I just gave the code use that one. You just need to replace the

PageReference pageRef = Page.YOURPAGENAME; >> PageReference pageRef = Page.sendEmailPage

 

And also keep in mind the sequence in which sample code is written.Sendemail needs to be initialised after you set the pageRef

Avidev9Avidev9
Also you have @isTest(SeeAllData=true) which is not a good practice
snapssnaps
I did that too, but then it said that the variable did not exist, which is
why I changed it to what I had.
Avidev9Avidev9
private class testSendEmail {
    private static testMethod void testSend() {
    	
        Opportunity testOpp = new Opportunity(name='Test Name', Ashoka_Website_Profile_Link__c = 'Some Link'); 
             insert testOpp;
PageReference pageRef = Page.SendEmailPage;
Test.setCurrentPage(pageRef);
ApexPages.currentPage().getParameters().put('id',testOpp.Id);
SendEmail se = SendEmail();
        
     	RecordType r = [SELECT ID from RecordType WHERE SObjectType ='Account' AND Name = 'Individual']; 
        Account acc = new Account(Name='Test Account', RecordType = r);
        insert acc;       
        Account a = [SELECT ID From Account WHERE Name = 'Test Account'];
        Contact con = new Contact(FirstName='Test',LastName='Contact', Email='test@ashoka.org', Account=a);
        insert con;
        Engagement_Role__c er = new Engagement_Role__c(Contact__c = con.Id, Opportunity__c= testOpp.Id, Role__c='Nominator');
        insert er;
             
        Opportunity updatedOpp = [SELECT Name, Ashoka_Website_Profile_Link__c,
            (SELECT Engagement_Role__c.Name,
                Engagement_Role__c.Contact__r.Email, 
                Engagement_Role__c.Contact__r.Name,
                Engagement_Role__c.Contact__r.Account.Name,
                Engagement_Role__c.Contact__r.Id
            FROM Opportunity.R00NR0000000UWbWMAW WHERE Role__c='Nominator' AND Engagement_Role__c.Contact__r.Email != NULL ) 
            FROM Opportunity WHERE Id = :testOpp.Id];
       
        System.assertEquals('Test Name', updatedOpp.Name);
        System.assertEquals('Some Link', updatedOpp.Name);
        System.assertEquals('test@ashoka.org', updatedOpp.R00NR0000000UWbWMAW[0].Contact__r.Email);
      	System.assertEquals('Test Contact', updatedOpp.R00NR0000000UWbWMAW[0].Contact__r.Name);
      	System.assertEquals(con.Id, updatedOpp.R00NR0000000UWbWMAW[0].Contact__r.Id);
        
        String nextPage = se.send().getUrl();
        PageReference page = se.send();
        System.assertEquals(':testOpp.Id', nextPage); 
        
        }
}

 

Avidev9Avidev9
What was the error ? Which variable
snapssnaps
With this line:

PageReference pageRef = Page.sendEmailPage;

I get "Compile Error: Variable does not exist: Page.sendEmailPage " which
is strange as I don't have a problem when I do
Test.setCurrentPageReference(new PageReference('Page.sendEmailPage'));
Avidev9Avidev9
Oops gave you the wrong syntax it should be

Pagereference pageref = Page.sendEmailPage;

Test.setCurrentPageReference(new PageReference('Page.sendEmailPage')); >> This is not correct , Even though it will compile but it wont point to the page