Annotation is not one the platform recognises
ID |
apex.correctness.nonexistent_annotations |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Suspicious Construct |
Language |
Apex |
Description
Reports an annotation at the start of a line whose name is not one of the platform’s. Only line-leading annotations are examined, so an email address or a literal containing @ is not reported, and the ApexDoc tags (@description, @param, …) are excluded by name because the matcher cannot see that they sit inside a comment.
Rationale
Apex does not reject an annotation it does not know - it ignores it. @isTets compiles exactly like @isTest and produces a method that is not a test: it never runs, it contributes nothing to coverage, and the class still deploys. The same is true of a misspelled @TestSetUp, which leaves every test in the class running without the data the method was written to create, and of @future written as @Future on a platform version that does not accept the spelling. The defect is invisible in the editor and in the deployment log, and shows up only as coverage that is lower than expected or as tests that fail for want of records.
The following code illustrates the pattern detected by this rule:
@IsTest
private class OrderServiceTest {
// FLAGGED: Annotation is not one the platform recognises
@isTets
static void discountIsApplied() {
System.assertEquals(1, 1, 'never runs as a test');
}
// FLAGGED: Annotation is not one the platform recognises
@TestSetp
static void seed() {
insert new Account(Name = 'Acme');
}
@IsTest
static void orderIsPersisted() {
System.assertEquals(1, 1, 'runs');
}
@TestSetup
static void setup() {
insert new Account(Name = 'Acme');
}
@SuppressWarnings('ApexUnusedDeclaration')
static String contactEmail() {
return 'someone@example.com';
}
/*******************************************************************************
@description
ApexDoc tags sit on their own line inside a block comment, which the matcher
cannot see - they must not be reported as unknown annotations.
@param accountId the record to load
@return the account
*/
@TestVisible
static Account load(Id accountId) {
return [SELECT Id FROM Account WHERE Id = :accountId];
}
}