+ Start a Discussion
saariko.ax241saariko.ax241 

Deploying to server - understanding Test procedure

Ok, so I have created my first Apex Class.

I was able to save it to my sandbox, and everything works as planned. I am getting an error when trying to deploy to my production server, ok, so I have created a test class, but I don't understand why/how and what is needed.

 

I get 2 main errors: 1. Assertion error

2. only 72% of code is covered.

 

I'd appreciate a pointer as to what's and how I should attack this.

 

My code inherits from inbound email:

 

My inboundEmail class:

 

global class inboundEmail implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();


    Account account;
    Lead lead;
String [] mFromUserParams; String [] sourceText; String mCaseObject; try{ sourceText = email.toAddresses[0].split('@'); String [] mParams = sourceText[0].split('\\.'); mFromUserParams = email.fromAddress.split('@'); mCaseObject = mParams[0]; if (mCaseObject == 'lead'){ lead = new Lead(); lead.LastName = mFromUserParams[0]; lead.Company = email.fromAddress; lead.OwnerId = mParams[1]; lead.LeadSource = mParams[2]; lead.Email = email.fromAddress; lead.RequirementsDescription__c = email.subject + email.plainTextBody; insert lead; result.success = true; } else if (mCaseObject == 'case'){ } else { result.success = false; } }catch(Exception e){ result.success = false; result.message = 'Oops, I failed.'; } return result; } }

 

My inboundEmailText class as currently configured: What I did, is create a new envelope, an email and a header, and populated all the fields that are to be populated as if a real email is coming. Is that what's the idea with testing?

 

Where is my problem?

@isTest
private class inboundEmailTest {

 // Success Case
   public static testMethod void inboundEmail(){
        
        // Create a new email, envelope object and Header
        Messaging.InboundEmail email = new Messaging.InboundEmail();
        Messaging.InboundEnvelope envelope = new Messaging.InboundEnvelope();
        Messaging.InboundEmail.Header hditem= new Messaging.InboundEmail.Header();

		email.headers = new Messaging.InboundEmail.Header[1];
		hditem.name='Date';
		hditem.value='Tue, 28 Apr 2009 14:08:37 -0700';
		email.headers[0]=hditem;

		envelope.toAddress = 'lead.idinboundmail@eihnp2ha.c-cpeneac.cl.apex.sandbox.salesforce.com';
        envelope.fromAddress = 'user@acme.com';
        email.subject = 'Please contact me';
        email.fromName = 'Test From Name';
        email.plainTextBody = 'Hello, this a test email body. for testing purposes only.Phone:123456 Bye';

        // setup controller object
        inboundEmail catcher = new inboundEmail();
        Messaging.InboundEmailResult result = catcher.handleInboundEmail(email, envelope);
        System.assertEquals( result.success  ,true);
    }
}

 

on deployment, I get that most lines from inboundEmail class are not covered.

 

As for the assertion test - shouldn't I check that the actual class did what it's supposed to?  why does the check must be: false?

 

 

Navatar_DbSupNavatar_DbSup

Hi,

Code coverage must be 75% for deploying from one org to another.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

saariko.ax241saariko.ax241

Hi Ankit,

 

I know that's the requirement -- but since I don't understand how to create the test  process to go over the missing lines, I can not improve the currently 72% coverage.

 

How can I achieve that goal?

craigmhcraigmh

On the Apex Class list page, it will show the code coverage percentage. Click on that, and you will see which lines are covered, and which are not. This will give you a much better idea as to what you need to add to your test class.

saariko.ax241saariko.ax241

Hi,

 

I don't have the code in front of my eyes now, but it's generally all the code the I wrote.

 

I do have an idea that I will check on Sunday: 

The object email.toAddresses is an array, and I populate it with a simple string. I will check that first thing.

 

 

saariko.ax241saariko.ax241

Ok, I tried with the email.toAddresses[0] but that's a nono.

 

Here is the error log I get:

 

# Test Results:
Average test coverage across all Apex Classes and Triggers is 72%, at least 75% test coverage is required.

 

The lines that are not covered are 27 - 44 (all are in the try/catch (is that a problem?)

*try{ is line 25

 

   try{
    	sourceText = email.toAddresses[0].split('@');
    	String [] mParams = sourceText[0].split('\\.');
    	mFromUserParams = email.fromAddress.split('@');
    	mCaseObject = mParams[0];
    	if (mCaseObject == 'lead'){  
	        lead = new Lead();
	        lead.LastName = mFromUserParams[0];
	        lead.Company = email.fromAddress;
	        lead.OwnerId = mParams[1]; 
	        lead.LeadSource = mParams[2];
	        lead.Email = email.fromAddress; 
	        lead.RequirementsDescription__c = email.subject + email.plainTextBody;
	        
	        insert lead;
	        result.success = true;
    	}  else if (mCaseObject == 'case'){
    		result.success = true;
    	}  else {
    		result.success = false;
    	}
    }catch(Exception e){

 

- Should I check if sourceText has values?

- What's the correct test method?

Currently, my test has very simple entries (as far as I understand of what is needed)

envelope.toAddress = 'lead.name.inboundmail@.c-cpeneac.cl.apex.sandbox.salesforce.com';
envelope.fromAddress = 'user@acme.com';
email.subject = 'Please contact me';
email.fromName = 'Test From Name';
email.plainTextBody = 'Hello, this a test email body. for testing purposes only.Phone:123456 Bye';    // setup controller object
inboundEmail catcher = new inboundEmail();
Messaging.InboundEmailResult result = catcher.handleInboundEmail(email, envelope);
System.assertEquals( result.success  ,false);

 

 

 

saariko.ax241saariko.ax241

I tried to add some deubg calls, ofc, nothing really shows up, as I still can not even validate the code.

 

I will appreciate any input from the community.

 

Thank you