You need to sign in to do that
Don't have an account?

Test class failing for Apex Email services
Hello everyone, I'm having trouble writing the test class for email services class. The functionality is we get automated emails in a particular format for service requests. Incoming email will be parsed through Apex email services and a case will be created. The Apex class is working very well, but I'm unable to succeed in test class. I've looked at various documents and resources online, but it only increased my confusion. Can any one please help me? Thank you!
Below is the Apex class, it's working fine with no errors.
Please check the test class. Resulting in "System.QueryException: List has no rows for assignment to SObject" error.
** This is how the incoming email looks like** Service Request #: 1-3747513422 RFS State: Waiting for ETA Vendor: Sample vendor Vendor Phone: (222) 943-1601 Trade: Sample Equipment Not to Exceed: $300 Service Need: Replacment Service Level: Critical - P1 Root Cause: Call Back Service Requested: 07/26/2018 02:58 PM Required Arrival Time: 07/26/2018 06:58 PM Target Completion Date: 07/27/2018 02:58 AM Email Delivery Time: 07/26/2018 02:58 PM Client: Test client #230 Address: 1490 S Main Jefferson, CA 11111 RFS Initiator Name: Test Manager Client Contact Phone: (111) 451-1686 Client Contact Fax: Store Contact: 333-928-3276 Store Number: 230
Below is the Apex class, it's working fine with no errors.
global Class EtoCaseservices implements Messaging.InboundEmailHandler { // Instantiate variables that we will need for handling this email String location; Integer locationIdx; String locationFinal; String need; String Inbox; String initiator; String Time; String po; String phone; String order; String critical; global Messaging.InboundEmailResult handleInboundEmail (Messaging.InboundEmail email, Messaging.InboundEnvelope envelope){ Messaging.InboundEmailResult result = new Messaging.InboundEmailResult(); if (email.fromaddress =='test@gmail.com') { loc = 'Client: '; locationIdx = email.plainTextBody.indexOf(location); String[] bodySplitted = email.PlainTextBody.split('\n'); locationFinal = email.plainTextBody.substring( locationIdx + location.length(), email.plainTextBody.indexOf('\n', locationIdx + location.length())); String[] locNme = locationFinal.split(' ', 3); phone = email.plainTextBody.substringBetween('Client Contact Phone: ' , 'Client Contact Fax: '); Inbox = email.plainTextBody.substringBetween('Email Delivery Time: ', 'Client: '); initiator = email.plainTextBody.substringBetween('RFS Initiator Name: ' , 'Client Contact Phone: '); Time = email.plainTextBody.substringBetween('Service Requested: ' , 'Email Delivery Time: '); po = email.plainTextBody.substringBetween('Service Request #: ' , 'RFS State: '); critical = email.plaintextbody.substringBetween('Service Level: ' , 'Root Cause: '); need = email.plaintextbody.substringBetween('Service Need: ' , 'Service Level: '); order = email.plaintextbody.substringBetween('Not to Exceed: ' , 'Service Need: '); system.debug('locationFinal: ' +locationFinal); if (locationFinal != NULL) { SVMXC__Site__c [] locArray = [Select Id, Name, SVMXC__Account__r.Id from SVMXC__Site__c where Name = :locationFinal]; try{ case c= new case(); c.subject= 'TEST REQUEST'; c.Case_Type__c= 'Service Request'; c.Origin='Email'; c.Status='new'; c.AccountId = locArray[0].SVMXC__Account__r.Id; c.SVMXC__Site__c = locArray[0].Id; c.recordtypeId = '012E0000000oRWX'; c.Description= critical + '\n' + need + '\n' + orderFinal + '\n' + timeFinal ; c.KA_PO__c= po; c.Location_Contact_Name__c = Initiator; c.BSP_Location_Contact_Phone__c = phone; insert c; } catch(System.dmlException e) {System.debug('Error: Unable to create new Case: ' + e); } } else if (locationFinal == null){ System.debug('No Location was found'); } } return result; } // Close handleInboundEmail () }
Please check the test class. Resulting in "System.QueryException: List has no rows for assignment to SObject" error.
@istest public class TestEtoCaseservices { static testMethod void TestEtoCaseservices () { // create a new email and envelope object Messaging.InboundEmail email = new Messaging.InboundEmail() ; Messaging.InboundEnvelope env = new Messaging.InboundEnvelope(); Account acc = new Account(); acc.Name = 'Test Smith'; insert acc; SVMXC__Site__c l = new SVMXC__Site__c(Name='Test Loc', SVMXC__City__c='Detroit', NTT_State__c='MI', SVMXC__Site_Phone__c='313 111 1231', SVMXC__Country__c='United States', NTT_Metro_Remote__c='ZONE A'); insert l; // setup the data for the email email.subject = 'Test Loc'; env.fromAddress = 'someaddress@email.com'; email.plainTextBody = 'This is just a test email for test class'; // call the email service class and test it with the data in the testMethod EtoCaseservices testInbound=new EtoCaseservices (); testInbound.handleInboundEmail(email, env); Messaging.InboundEmailResult result = testInbound.handleInboundEmail(email, env); System.assertEquals( result.success ,true); Case c = [select id, account.id, SVMXC__Site__c, recordtypeId, KA_PO__c, Location_Contact_Name__c, description, BSP_Location_Contact_Phone__c, subject, Case_Type__c, Origin, Status, AccountId from Case where SVMXC__Site__c = :l.Id]; System.assertEquals(c.SVMXC__Site__c, l.Id); System.assertEquals(c.account.Id, acc.Id); System.assertEquals(c.recordtypeId, '012E0000000oRWX'); System.assertEquals(c.KA_PO__c, '10010101'); System.assertEquals(c.Location_Contact_Name__c, 'Test Man'); System.assertEquals(c.BSP_Location_Contact_Phone__c, '111 222 3443'); System.assertEquals(c.subject, 'Test Request'); System.assertEquals(c.Case_Type__c, 'Service Test'); System.assertEquals(c.Origin, 'Email'); System.assertEquals(c.Status, 'New'); System.assertEquals(c.description, 'Test description'); update c; } }
set the subject of the test class ..... and add the all values
email.plainTextBody = 'This is just a test email for test class . Client:Test Loc \n Client Contact Phone:12312323 ';
Remove recordtypeId from the hardcoded values