+ Start a Discussion
ACPreveauxACPreveaux 

Apex InboundEmail Service test NullPointerException

I'm trying to test an email handler which updates a custom object in my org.  It works successfully in practice, but the test case throws an exception "System.NullPointerException: Argument cannot be null." when saving.  The PlainTextBody in the test case is copied verbatim from a successful email (with the exception that new lines are substituted with "\n" in test code).
Class Code:
global class SF_Title implements Messaging.InboundEmailHandler {
	global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
		Messaging.InboundEmailResult result = New Messaging.InboundEmailResult();
/*Insert email handling code here*/
		//Here we want to add a new title to the 'Titles' Object
		//Create the new title record
		Title__c newTheme = new Title__c();
		//and define a map variable to store the fields we want to include in the insert
		map<string,string> fields = new map<string,string>();
		/*Here I split the email body by newLine and iterate over those lines to create key/value pairs
		 *which then get split and inserted into a map for calling during the field assignment phase.
		 */
		for (string record: email.plainTextBody.split('\n',0)) {
			try{ 
				list<string> KeyValue = record.split(': ',0);
				String Key = KeyValue[0];
				String Value = KeyValue[1];
				fields.put(Key,Value);
				
			} catch(exception e) {
				system.debug(e);//pass if no value associated with key
			}
		}
		
		system.debug(email.plainTextBody);
		
		system.debug(fields);
		//Field assignment phase
		newTheme.OwnerId = '005C0000003fVL3IAM'; //User ID for Allen Preveaux
		newTheme.Name				=fields.get('Title'); //type: Text(80)
		try {
			newTheme.Perc_Payback86__c	= Decimal.valueOf(fields.get('86%')); //type: Percent(2,2)
		} catch(exception e) {
		}
		try{
			newTheme.Perc_Payback87__c	=Decimal.valueOf(fields.get('87%')); //type: Percent(2,2)
		} catch(exception e) {
		}
		try{
			newTheme.Perc_Payback88__c	=Decimal.valueOf(fields.get('88%')); //type: Percent(2,2)
		} catch(exception e) {
		}
		try{
			newTheme.Perc_Payback89__c	=Decimal.valueOf(fields.get('89%')); //type: Percent(2,2)
		} catch(exception e) {
		}
		try{
			newTheme.Perc_Payback90__c	=Decimal.valueOf(fields.get('90%')); //type: Percent(2,2)
		} catch(exception e) {
		}
		try{
			newTheme.Perc_Payback92__c	=Decimal.valueOf(fields.get('92%')); //type: Percent(2,2)
		} catch(exception e) {
		}
		try { 
			newTheme.Perc_Payback94__c	=Decimal.valueOf(fields.get('94%')); //type: Percent(2,2)
		} catch(exception e) {
		}
		try{ 
			newTheme.Perc_Payback95__c	=Decimal.valueOf(fields.get('95%')); //type: Percent(2,2)
		} catch(exception e) {
		}
		try{ 
			newTheme.Class__c 			=fields.get('Class'); //type: Picklist
		} catch(exception e) {
		}
		try{ 
			newTheme.Cover_Cost__c		=fields.get('Lines'); //type: Picklist
		} catch(exception e) {
		}
		try{
			newTheme.Force_Bet__c		=Boolean.valueOf(fields.get('Force Bet')); //type: Checkbox
		} catch(exception e) {
		}
		try{
			newTheme.Legal_Config__c	=fields.get('Legal Configuration'); //type: text(4)
		} catch(exception e) {
		}
		try{
			newTheme.MLPs__c			=Boolean.valueOf(fields.get('MLPs')); //type: Checkbox
		} catch(exception e) {
		}
		try{
			newTheme.Per_Diem__c		=Decimal.valueOf(fields.get('Per Diem')); //type: Number(3,2)
		} catch(exception e) {
		}
		try{
			newTheme.Product_Type__c	=fields.get('Product Type'); //type: Picklist
		} catch(exception e) {
		}
		try{
			newTheme.ProgId__c			=fields.get('ProgID'); //type: Text(15)
		} catch(exception e) {
		}
		try{
			newTheme.Skin_Family__c		=fields.get('Skin Family'); //type: Text(40)
		} catch(exception e) {
		}
		try{
			newTheme.Software_Req__c	=fields.get('Software Required'); //type: Text(40)
		} catch(exception e) {
		}
		try{
			newTheme.MaxBet__c			=Decimal.valueOf(fields.get('Typical Max Bet')); //type:  Currency(3,2)
		} catch(exception e) {
		}
		try{
			newTheme.VidCard__c			=Boolean.valueOf(fields.get('Video Card Required')); //Type: Checkbox
		} catch(exception e) {
		} 
		//perform the insert, and there you have it
		system.debug(newTheme);  //testing command.  Comment/Remove upon official deployment
		try {
			insert newTheme;
		} catch (DmlException e) {
			System.debug('The following error has occurred: '+e.getMessage());
		}
/*End of cool ideas*/
		return result;
	}
}
Test Code:
@istest
public without sharing class Test_SF_Title {
	static testmethod void test_email_title_updater(){
		Messaging.InboundEmail emailAllFields = new Messaging.InboundEmail();
		Messaging.InboundEnvelope envelope = new Messaging.InboundEnvelope();
		
		emailAllFields.plainTextBody = 'Title: Test Title Update 1 \n 86%: 86.05 \n 87%: 87.07 \n 88%: 88.04 \n 89%: 88.97 \n 90%: 89.99 \n 92%: 92.01 \n 94%: 93.97 \n 95%: 95.02 \n Class: 2 \n Lines: 50 \n Force Bet: true \n Legal Configuration: 8378 \n MLPs: True \n Per Diem: 25 \n Product Type:  Core \n ProgID: 117 \n Skin Family: White Buffalo \n Software Required: V14.2.1_C3/r74835 \n Typical Max Bet: 5.00 \n Video Card Required:  True';
		envelope.fromAddress = 'testUser@playags.com';
		SF_Title title_update_tester= new SF_Title();//now I need to include an instance of the SF Title handler and process the above
		title_update_tester.handleInboundEmail(emailAllFields,envelope);
		
		
		//also needed is a version with all exceptions and we need to process that one too to test all of the catch portions of the code. 
	}
}

Test class complains about the NullPointerException on the line where I provide 'title_update_tester.handleInboundEmail(emailAllFields,envelope);'.

I've tried eliminating the spaces between the fields and '\n's, adding fields to the emailAllFields parameter, and moving the fromAddress from envelope to emailAllFields.
Amit Chaudhary 8Amit Chaudhary 8
Please check below blog. I hope that will help u
http://srinivas4sfdc.blogspot.in/2013/11/test-class-example-for.html

Please let us know if this will help you

Thanks
Amit Chaudhary