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 = 'exampleemail@domain.com';

// 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

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 = 'exampleemail@domain.com';

  // 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
    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. – MattGarnett Apr 12 '18 at 12:29
  • @MattGarnett Yes, testMethod should not be used. – sfdcfox Apr 12 '18 at 18:06
  • @sfdcfox Hi fox, so what's the alternative here out of interest? – MattGarnett Apr 12 '18 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 Apr 12 '18 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.