You need to sign in to do that
Don't have an account?
snaps
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?
It seems like you never sent the url param that you are using to do the query
Try something like this
Thank you for your response. I tried your suggestion but unfortunately my code coverage went down to 0%. I tried inserting this:
But that didn't work. So I tried using the page with a specific ID:
Not surprisingly, that didn't work either.
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
why I changed it to what I had.
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'));
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