You need to sign in to do that
Don't have an account?
Shawn Reichner 33
Help with Test Class for Apex Incoming Email Service where Custom Object is Created
Hello awesome developers!
I have the following Apex class that implements an incoming Apex Email Service. The class works 100% with no issues and I get a Custom Object record creatd and all is good. The issue is when I attempted to create a test class for this service. I get the lines of the email service covered but the record being created is not being covered. This is due to in my test class below you will notice the SOQL query to bring back the custom object record that was created by the test class to assert against and there are no rows for assignment.
Can you kindly help me update the beloe test class to cover 100% and to cover the custom object record that is being created.
Thank you so much in advance for any help you are able to provide,
Shawn
Class:
Test Class:
I have the following Apex class that implements an incoming Apex Email Service. The class works 100% with no issues and I get a Custom Object record creatd and all is good. The issue is when I attempted to create a test class for this service. I get the lines of the email service covered but the record being created is not being covered. This is due to in my test class below you will notice the SOQL query to bring back the custom object record that was created by the test class to assert against and there are no rows for assignment.
Can you kindly help me update the beloe test class to cover 100% and to cover the custom object record that is being created.
Thank you so much in advance for any help you are able to provide,
Shawn
Class:
global class SFDCIssueEmailClass implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){ Messaging.InboundEmailResult result = new Messaging.InboundEmailResult(); String myPlainText= ''; Group queue = [SELECT Id FROM Group WHERE Name = 'Salesforce Queue' and Type='Queue']; myPlainText = email.plainTextBody; Salesforce_Issue__c[] newTask = new Salesforce_Issue__c[0]; try { User vCon = [SELECT Id, FirstName, LastName, Email FROM User WHERE Email = :email.fromAddress LIMIT 1]; newTask.add(new Salesforce_Issue__c(Descripton__c = myPlainText, Priority__c = 'P4', Status__c = 'New', Subject__c = email.subject, Email_Requestor__c = email.fromAddress, OwnerId = queue.Id, Origin__c = 'Email', RecordTypeId = '012q00000005mXhAAI')); insert newTask; System.debug('New Task Object: ' + newTask ); } catch (QueryException e) { System.debug('Query Issue: ' + e); } result.success = true; return result; } }
Test Class:
@isTest public class SFDCIssueEmailClassTest { public static testMethod void EmailTest(){ Contact c = new Contact(lastname = 'tested', email = 'test@test.com'); insert c; SFDCIssueEmailClass objconfirm = new SFDCIssueEmailClass(); Messaging.InboundEmail email = new Messaging.InboundEmail(); email.fromAddress = c.email; email.subject = 'Test Subject'; email.plainTextBody = 'my text'; // objconfirm.handleInboundEmail(email, envelope); Messaging.InboundEnvelope envelope = new Messaging.InboundEnvelope(); objconfirm.handleInboundEmail(email, envelope); Messaging.InboundEmailResult result = objconfirm.handleInboundEmail(email, envelope); system.assertEquals(result.success, true); Salesforce_Issue__c[] issue = [SELECT ID FROM Salesforce_Issue__c WHERE Email_Requestor__c = :email.fromAddress]; system.assertEquals(1, issue.size(),'Issue not inserted'); } }
Try use the following sample as a reference.
All Answers
Try use the following sample as a reference.