2

I'm brand new to Apex/Salesforce development, and am attempting to create some Apex triggers and tests. Each time I try to add a new class, when I start it with @IsTest I am unable to save it. I see the error: "Error: Compile Error: unexpected token: @ at line 1 column 0".

Is there somewhere else I'm supposed to put tests for triggers? Or something else I should be doing?

Thanks.

  • Are you creating a brand new test class or are you trying to add this directly to the trigger? – Chris Duncombe Oct 14 '14 at 22:26
  • Brand new class. I have CheckLead (as one class, where the trigger is) and CheckLead_test, where line 1 is @IsTest – Jonathan Stegall Oct 14 '14 at 22:30
  • What IDE Are you using? What API Version? – Adrian Larson Oct 14 '14 at 23:48
  • I'm just using the Developer Console from Setup. API version seems to be 31. – Jonathan Stegall Oct 15 '14 at 12:54
  • @AdrianLarson I believe you have pointed me in the right direction. I spent some time installing/setting up the Force.com IDE/Eclipse, and I do not see the error there. Further, the trigger for which I was trying to write a test now says it is 100% covered. Would you want to make an answer so I can mark it as correct? – Jonathan Stegall Oct 15 '14 at 16:35
5

Triggers can't have annotations, and while they can contain classes, those classes can't be "special" classes, such as @RestResource classes. You need to make a class to test the trigger. Do not confuse a class with a trigger. A trigger looks like a class, but has different semantics.

The following code won't compile:

@isTest trigger MyTrigger on Case (after insert) { ...

Nor will this:

trigger MyTrigger on Case (after insert) {
    @isTest class TestTrigger { ...

Instead, make your trigger:

trigger X on Y (...) { ...

And a class for that trigger:

@isTest class TestTriggerX {
     @isTest static void testTrigger() { ...

Classes are made in Setup > Develop > Apex Classes, while triggers are made on the object (e.g. Setup > Customize > Accounts > Triggers). The word "class" generally should never appear in your triggers, and the word "trigger" should generally never appear in your classes.

You can't combine multiple top-level elements into a single class or trigger.

| improve this answer | |
  • Currently, I have CheckLead_test as an Apex Class (as you say) and CheckLead as a trigger. CheckLead_test starts like this: @isTest class CheckLead_Test { static testMethod void insertNewLead() { – Jonathan Stegall Oct 15 '14 at 12:45
0

In case other folks find this one, the problem was with the Developer Console (at least in our case it was). I was unable to create new test classes in there, but instead had to install Eclipse and Force.com package and deploy from there.

To be clear, I can run tests in Developer Console, but have to create them in Eclipse.

| improve this answer | |

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.