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
Maf_007Maf_007 

Email Service Test class Code Coverage

Hi,

 

I was wondering if anyone could help me on the following test class as I am getting an error message and getting only 52% code coverage:

 

My Email Service Class is:

global class EmailDemoReceive implements Messaging.InboundEmailHandler {
  global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, 
                                                         Messaging.Inboundenvelope envelope) {
  	
    Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
                                                             
    //retrieve all eco survey where it matches with the email subject.... 
    List<Opportunity> existingsurveys = [select id, Job_Reference__c from Opportunity where Job_Reference__c = :email.subject];
    Map<id,Opportunity> ES = new Map<id,Opportunity>{};
    ID ESid;
        
        for(Opportunity Opp: existingsurveys){
        	//ES.put(Opp.id, Opp);
            ESid = Opp.id;
        }
    try{
        //check Eco Survey for an existing record with same reference and there's an attachment
        if(existingsurveys.size() > 0){
            // Save attachments
            if(email.binaryAttachments!=null && email.binaryAttachments.size()>0){
            	for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
  					Attachment attachment = new Attachment();
                    String checker = bAttachment.filename;
 
                    //only accept xml attachments..
                    if(checker.contains('.xml')||checker.contains('.pdf')){
                    	attachment.Name = bAttachment.fileName;
  						attachment.Body = bAttachment.body;
  						attachment.ParentId = ESid;
                		//attachment.ParentId = ES.get(EcoSurvey__c).id;
  						insert attachment;
                    }
                    else{
                    	continue;
                    }
				}
            }
        }
    
    }Catch(QueryException e){
    
    }
    return result;
    }
}

 

And Test Class Is:

 

@isTest(SeeAllData=true)
public class TestEmailAttachment{
	public static testMethod void EmailAttachmentTester(){
        
        // setup controller object
        EmailDemoReceive objconfirm = new EmailDemoReceive();
        System.debug('Im starting a method');
        
        //Create an Account for the opportunity to be created
        Account acc = new Account();
        acc.Name = 'M.Choudhury';
        insert acc;
        
        
        //create a new opportunity with email subject as job reference number
        Opportunity Opp = new Opportunity();
        Opp.Name = 'tasty test';
        Opp.Accountid = acc.id;
        Opp.StageName = 'Pre Qualification';
        Opp.closedate = Date.Today()+2;
        insert Opp;
        
        Opportunity testop1 = [select id, Job_Reference__c from Opportunity where id = :Opp.id];
        
        // Create a new email, envelope object and Attachment
        Messaging.InboundEmail email = new Messaging.InboundEmail();
        Messaging.InboundEnvelope envelope = new Messaging.InboundEnvelope();
        objconfirm.handleInboundEmail(email, envelope);
        
        email.subject = testop1.Job_Reference__c;
        email.plainTextBody = 'Hello, this a test email body. for testing purposes only. Bye';
        envelope.fromAddress = 'maf@cloudsocius.com';
        Messaging.InboundEmail.BinaryAttachment[] binaryAttachments = new Messaging.InboundEmail.BinaryAttachment[2];
        Messaging.InboundEmail.BinaryAttachment binaryAttachment = new Messaging.InboundEmail.BinaryAttachment();
		binaryAttachment.Filename = 'test.pdf';
        
        Messaging.InboundEmail.BinaryAttachment binaryAttachment1 = new Messaging.InboundEmail.BinaryAttachment();
        binaryAttachment1.Filename = 'test.xml';
        
        
        Messaging.InboundEmailResult result = objconfirm.handleInboundEmail(email, envelope);
        System.assertEquals( result.success  ,true);
        
        update testop1;
        
        //Opportunity testop = [select id, Job_Reference__c from Opportunity where id = :testop1.id];
        
        Attachment att = [Select id, name, parentid from Attachment where parentid = :testop1.id and name = 'test.pdf'];
        System.assertEquals(binaryAttachment.Filename,att.name);
    
    }
}

 I get the following error Message:

 

System.QueryException: List has no rows for assignment to SObject

Class.TestEmailAttachment.EmailAttachmentTester: line 48, column 1

 

Best Answer chosen by Admin (Salesforce Developers) 
Maf_007Maf_007
@isTest(SeeAllData=true)
public class TestEmailAttachment{
	public static testMethod void EmailAttachmentTester(){
        
        // setup controller object
        EmailDemoReceive objconfirm = new EmailDemoReceive();
        System.debug('Im starting a method');
        
        //Create an Account for the opportunity to be created
        Account acc = new Account();
        acc.Name = 'M.Choudhury';
        insert acc;
        
        
        //create a new opportunity with email subject as job reference number
        Opportunity Opp = new Opportunity();
        Opp.Name = 'tasty test';
        Opp.Accountid = acc.id;
        Opp.StageName = 'Pre Qualification';
        Opp.closedate = Date.Today()+2;
        insert Opp;
        
        Opportunity testop1 = [select id, Job_Reference__c from Opportunity where id = :Opp.id];
        
        // Create a new email, envelope object and Attachment
        Messaging.InboundEmail email = new Messaging.InboundEmail();
        Messaging.InboundEnvelope envelope = new Messaging.InboundEnvelope();
        objconfirm.handleInboundEmail(email, envelope);
        
        email.subject = testop1.Job_Reference__c;
        email.plainTextBody = 'Hello, this a test email body. for testing purposes only. Bye';
        envelope.fromAddress = 'maf@cloudsocius.com';
        //Messaging.InboundEmail.BinaryAttachment[] binaryAttachments = new Messaging.InboundEmail.BinaryAttachment[2];
        Messaging.InboundEmail.BinaryAttachment binaryAttachment = new Messaging.InboundEmail.BinaryAttachment();
		binaryAttachment.Filename = 'test.pdf';
        binaryAttachment.body = blob.valueOf('my attachment text');
        //binaryattachment.mimeTypeSubType = 'text/plain';
        email.binaryAttachments = new Messaging.inboundEmail.BinaryAttachment[] { binaryattachment };
        objconfirm.handleInboundEmail(email, envelope);
        
        Messaging.InboundEmailResult result = objconfirm.handleInboundEmail(email, envelope);
        System.assertEquals( result.success  ,true);
        
        update testop1;
        
        Opportunity testop = [select id, Job_Reference__c from Opportunity where id = :testop1.id];
        
        List<Attachment> att = [Select id, name from Attachment where parentid = :testop.id and name = :'test.pdf'];
        System.assertEquals(att[0].name, 'test.pdf');
    
    }
}

 Here is The Solution Guys. Thanks if anyone Looked at this post