I wrote a trigger to strip HTML out of a field (the long text field gets copied from a rich text field and comes in with the HTML). I know it works, I just want to write the test class to verify.

Here is the Apex Trigger

trigger removeHTML on Case (before update) {
    for (Case c : Trigger.New){
    c.Field_History__c = c.Field_History__c.stripHtmlTags();

Here is what I have for the test:

public class removeHTML_Test {
    Case mycase = [select Id from Case where Id = 'TheIDofaCaseIwantToUseforTesting'];        
    mycase.Field_History__c = '<p>New Update</p>';
    update mycase;

Line 4 Unexpected token '='. Line 5 Unexpected token ';'.

I have never written a test class before so any help would be appreciated.

1 Answer 1


Your test class has code outside of a method, which is generally not syntactically valid (not for test classes, and not for regular classes either).

Put your code inside an @isTest annotated method, and the errors should disappear.


private class MyTestClass{
    static void myTestMethod(){
        Case c = [SELECT Id FROM Case];
        c.Subject = 'foo';
        // ...etc

You generally need to create your own test data for unit tests due to test isolation (which is enabled by default, and this is a good thing). So be sure to create a case in your test class (like in a @testSetup annotated method) before you try to query it. Hard-coding Ids is generally a red flag.

Don't forget to make assertions, and to write "negative tests" (tests which stress what happens when you get unexpected/bad/missing input). Your trigger will currently encounter a Null Pointer Exception if Field_History__c is blank. Write the negative test to verify that, then adjust your trigger to handle that situation (and stop tweaking it once your negative test passes).

  • Thanks Derek. That did the trick. Commented Nov 17, 2021 at 17:27

You must log in to answer this question.

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