You need to sign in to do that
Don't have an account?
Akash
Attachments in inboundemail
Hi,
I am trying to attach an attachment in email for test method but I am not able to attach it. My code for email method is:
global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,Messaging.InboundEnvelope env)
{
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
string FileNames='';
Integer RecCounter = 0 ;
Integer ColCounter = 0 ;
List<Messaging.InboundEmail.TextAttachment> Obj = new List<Messaging.InboundEmail.TextAttachment>();
string UniquePhoneNumbers = '';
if (email.textAttachments != null)
{
//Attahcment found...
Obj = email.textAttachments;
Integer i;
}
}
and test method code is:
public static testMethod void testTasks() {
// Create a new email and envelope object
Messaging.InboundEmail email = new Messaging.InboundEmail();
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
List<Messaging.InboundEmail.TextAttachment> objList = new List<Messaging.InboundEmail.TextAttachment>();
Messaging.InboundEmail.TextAttachment obj = new Messaging.InboundEmail.TextAttachment();
obj.body = 'test';
obj.fileName = 'C:\test.vcf';
obj.mimeTypeSubType = 'application/octet-stream';
objList.add(obj);
email.textAttachments = objList;
}
The test method is not taking the attachment.
Can you please help me in attaching an attachment in my test method? Please treat this as very urgent.
Thanks
I am trying to attach an attachment in email for test method but I am not able to attach it. My code for email method is:
global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,Messaging.InboundEnvelope env)
{
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
string FileNames='';
Integer RecCounter = 0 ;
Integer ColCounter = 0 ;
List<Messaging.InboundEmail.TextAttachment> Obj = new List<Messaging.InboundEmail.TextAttachment>();
string UniquePhoneNumbers = '';
if (email.textAttachments != null)
{
//Attahcment found...
Obj = email.textAttachments;
Integer i;
}
}
and test method code is:
public static testMethod void testTasks() {
// Create a new email and envelope object
Messaging.InboundEmail email = new Messaging.InboundEmail();
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
List<Messaging.InboundEmail.TextAttachment> objList = new List<Messaging.InboundEmail.TextAttachment>();
Messaging.InboundEmail.TextAttachment obj = new Messaging.InboundEmail.TextAttachment();
obj.body = 'test';
obj.fileName = 'C:\test.vcf';
obj.mimeTypeSubType = 'application/octet-stream';
objList.add(obj);
email.textAttachments = objList;
}
The test method is not taking the attachment.
Can you please help me in attaching an attachment in my test method? Please treat this as very urgent.
Thanks
I don't think that is going to work.
what is the error you see?
What do you expect to happen ?
attachments normally come from within the platform
{
string FileNames='';
Integer RecCounter = 0 ;
Integer ColCounter = 0 ;
try
{
List<Messaging.InboundEmail.TextAttachment> Obj = new List<Messaging.InboundEmail.TextAttachment>();
string UniquePhoneNumbers = '';
if (email.textAttachments != null)
{
//Attahcment found...
Obj = email.textAttachments;
Integer i;
{
if (FileNames=='')
FileNames=Obj[i].fileName;
else
FileNames=FileNames +','+ Obj[i].fileName;
}
}
}
..........
Messaging.InboundEmail email = new Messaging.InboundEmail();
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
List<Messaging.InboundEmail.TextAttachment> objList = new List<Messaging.InboundEmail.TextAttachment>();
Messaging.InboundEmail.TextAttachment obj = new Messaging.InboundEmail.TextAttachment();
obj.fileName = 'test.txt';
obj.mimeTypeSubType = 'text';
obj.charset = 'utf-8';
taskObj.handleInboundEmail(email, env);
the other method is to perform all your logic to deal with a text attachment in a separate method that you can then call directly from your testMethod
something like this :
Messaging.InboundEmailResult handleInboundEmail() {
....
if (email.textAttachments != null) { processTextAttachment( email.textAttachments) }
...
}
then move your untested code to the new function processTextAttachment()
then in your test method, call this new function directly
testMethod t1() {
...
obj.body = 'test';
obj.fileName = 'test.txt';
obj.mimeTypeSubType = 'text';
obj.charset = 'utf-8';
processTextAttachment( obj)
}
now you can easily test code that otherwise would not get covered.
i'm really stumped as to why you don't see textAttachments as not null, but refactoring to add testability is perfectly valid solution.
http://wiki.apexdevnet.com/index.php/Code_Sample_-_Testing_Email_Services_with_Inbound_Attachments