0

I am trying to write a test class for my custom Email Listener class but receive an error message

unexpected token: '=' at line 11.

Can someone point out to be me what's wrong in my test class?

Kind regards, Chris

Custom Email Listener (API Version 32):

global class CSS_Email implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,
            Messaging.InboundEnvelope env) {

        // Create an InboundEmailResult object for returning the result of the
        // Apex Email Service
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();

        String myText = '';

        // Add the email text into the local variable
        myText = email.htmlBody;

        // New Task object to be created
        Task__c[] newTask = new Task__c[0];

        // Create the new task
        newTask.add(new Task__c(Details__c = myText,
                                Priority__c = 'Unassigned',
                                Status__c = 'New',
                                Due_Date__c = Date.Today(),
                                Requestor_email__c = email.fromAddress,
                                Short_Description__c = email.subject,
                                OwnerId = '00GN0000000ytuUMAQ'));

        // Insert the new Task
        insert newTask;

        if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
            List<Attachment> attList = new List<Attachment>();
            // This will extract all blob attachments from email

            for (Messaging.Inboundemail.BinaryAttachment file : email.binaryAttachments) {
                Attachment attachment = new Attachment();
                attachment.Name = file .fileName;
                attachment.Body = file .body;

                // some hardcoded or id extracted from email reference
                attachment.ParentId = newTask[0].ID;
                attList .add(attachment);
            }
            if (attList .size() > 0) {
                insert attList;
            }
        }
        return result;
    }
}

Test Class (API Version 32):

@IsTest
private class CSS_Email_Test {
        // Create a new email and envelope object
        Messaging.InboundEmail email  = new Messaging.InboundEmail();
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();

        // Set up your data if you need to

        // Create the email body
        email.plainTextBody = 'This should become a note';
        email.fromAddress ='[email protected]';
        String contactEmail = '[email protected]';
        email.ccAddresses = new String[] {'Jon Smith <' + contactEmail + '>'};
        email.subject = 'Dummy Account Name 123';

        CSS_Email edr = new CSS_Email();

        Test.startTest();
        Messaging.InboundEmailResult result = edr.handleInboundEmail(email, env);
        Test.stopTest();

    }
}
4
  • What is written on the line no. 11
    – Ashwani
    Commented Feb 18, 2015 at 6:37
  • What is new Task__c[0] supposed to be doing?
    – BarCotter
    Commented Feb 18, 2015 at 9:58
  • new Task__c[0] is to create a new task ticket upon receiving email.
    – user16806
    Commented Feb 18, 2015 at 22:59
  • @Ashwani what's wrong with line 11?
    – user16806
    Commented Feb 18, 2015 at 23:00

2 Answers 2

0

is it:

new List{'Jon Smith <' + contactEmail + '>'};

instead of ?

new String[] {'Jon Smith <' + contactEmail + '>'};

0

Ok, I am a noob in writing the test class.

What is missing is the function within the class. Here is the solution which can cover 47% of my email listener class. I will expand the test class to include attachment which will made this code 100% coverage:

@IsTest private class CSS_Email_Test { static testMethod void testUnsubscribe() {

    // Create a new email and envelope object
    Messaging.InboundEmail email  = new Messaging.InboundEmail();
    Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();

    // Set up your data if you need to

    // Create the email body
    email.subject = 'Testing 1234';
    env.fromAddress ='[email protected]';
    email.plainTextBody = 'This should become a note';
    String contactEmail = '[email protected]';

    CSS_Email CSS_EmailObjTst = new CSS_Email();

    Test.startTest();
    CSS_EmailObjTst.handleInboundEmail(email, env);
    Test.stopTest();      

} 

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .