3

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.

5
  • Are you creating a brand new test class or are you trying to add this directly to the trigger? Commented Oct 14, 2014 at 22:26
  • Brand new class. I have CheckLead (as one class, where the trigger is) and CheckLead_test, where line 1 is @IsTest Commented Oct 14, 2014 at 22:30
  • What IDE Are you using? What API Version?
    – Adrian Larson
    Commented Oct 14, 2014 at 23:48
  • I'm just using the Developer Console from Setup. API version seems to be 31. Commented Oct 15, 2014 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? Commented Oct 15, 2014 at 16:35

2 Answers 2

6

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.

2
  • 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() { Commented Oct 15, 2014 at 12:45
  • If this was possible, it would be quite a nice way to test Platform Events have been published. Because you could have a @isTest subscriber trigger class...
    – Robs
    Commented Oct 31, 2020 at 14:15
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.

You must log in to answer this question.

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