2

What is wrong with this test class?

I'm getting the error:

Error: Compile Error: Unexpected token '='. at line 7 column 19

This is at "email.subject = ..."

What is possibly wrong with this assignment? It's exactly as described in Salesforce coding guidelines.

@IsTest
public class NewRelicInboundEmailAlerts_Test {

Messaging.InboundEmail email = new Messaging.InboundEmail();
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();

email.subject = 'FW: Incident 20216330';
env.fromAddress = '[email protected]';

// add an Binary attachment
Messaging.InboundEmail.BinaryAttachment attachment = new Messaging.InboundEmail.BinaryAttachment();
attachment.body = blob.valueOf('test');
attachment.fileName = 'bd27b20b74e689b19eacd89ed7a2a62decba7356.json';
attachment.mimeTypeSubType = 'application/json';

email.binaryAttachments = new Messaging.inboundEmail.BinaryAttachment[] { attachment };

// call the email service class and test it with the data in the testMethod
NewRelicInboundEmailAlerts testEmailAlert = new NewRelicInboundEmailAlerts();
testEmailAlert.handleInboundEmail(email, env);
}

1 Answer 1

1

This is because you can't directly use them in class. This is not the correct format.

You need methods to assign them value.

public static testmethod testOne() {
  Messaging.InboundEmail email = new Messaging.InboundEmail();
  Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();

  email.subject = 'FW: Incident 20216330';
  env.fromAddress = '[email protected]';

  // add an Binary attachment
  Messaging.InboundEmail.BinaryAttachment attachment = new Messaging.InboundEmail.BinaryAttachment();
  attachment.body = blob.valueOf('test');
  attachment.fileName = 'bd27b20b74e689b19eacd89ed7a2a62decba7356.json';
  attachment.mimeTypeSubType = 'application/json';

  email.binaryAttachments = new Messaging.inboundEmail.BinaryAttachment[] { attachment };

  // call the email service class and test it with the data in the testMethod
  NewRelicInboundEmailAlerts testEmailAlert = new NewRelicInboundEmailAlerts();
  testEmailAlert.handleInboundEmail(email, env);

}
4
  • 1
    Absolutely, quite literally just figured this out looking at someone else's answer. It's confusing as they said using testMethod is deprecated now in guidelines? Anyway - thanks so much for the answer. Commented Apr 12, 2018 at 12:29
  • @MattGarnett Yes, testMethod should not be used.
    – sfdcfox
    Commented Apr 12, 2018 at 18:06
  • @sfdcfox Hi fox, so what's the alternative here out of interest? Commented Apr 12, 2018 at 18:44
  • 2
    @MattGarnett @isTest should be used both to identify test classes and methods. See this document: "The testMethod keyword is now deprecated. Use the \@isTest annotation on classes and methods instead."
    – sfdcfox
    Commented Apr 12, 2018 at 19:11

You must log in to answer this question.

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